coldfusionlucee

how can I use cfdirectory to make a list of documents downloadable using ColdFusion/Lucee?


In my Application.cfc, I setup a mapping

this.mappings["/downloads"]="J:\Downloads\documents";

In my template, I have

<cfdirectory action="list" directory="#expandpath("/downloads")#" filter="*.zip|*.docx" name="downloads" recurse="yes">
<!--- <cfdump var="#expandpath("/software")#"> --->
<cfdump var="#downloads#">
<ul>
    <cfoutput query="#downloads#">
        <li><a href="#downloads.directory#/#downloads.name#">#downloads.name#</a></li>
    </cfoutput>
</ul>

I'm trying to make the documents downloadable but when the link is clicked, nothing is happening which makes me think my links are not correct however when I mouse over the link, I see the full path which is correct.

What am I missing to make the list of documents clickable?

enter image description here

Here is the URL displayed when mouseover the 3rd document for example.

enter image description here


Solution

  • Since the files are outside of your webroot you will need to have ColdFusion read the file and send it back to the browser.

    You will need to create a page, like download.cfm, that can accept a URL parameter to know which file to access. Once you have selected the file you can use something like the following to stream the file.

    <cfheader name="Content-disposition" value="attachment;filename=#datafile#">
    <cfcontent file="#datafile#" type="application/pdf">
    

    The above code was pulled from https://www.raymondcamden.com/2006/03/10/Ask-a-Jedi-Using-ColdFusion-to-serve-files

    WARNING: Reading URL parameters in this way and giving people access to the filesystem is extremely unsafe. Safer alternatives should be considered before moving something like this into a production environment.