HPWD
HPWD

Reputation: 2240

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

Upvotes: 0

Views: 314

Answers (2)

HPWD
HPWD

Reputation: 2240

All I needed to do for this exercise is to setup a mapping in my Application.cfc. As others have stated, there is zero security here but for the purpose of this exercise of understanding virtual directories (IIS) and aliases (CommandBox), this is sufficient.

this.mappings["/guides"]="J:\guides";

Then I can use cfdirectory to build my query object

<cfdirectory action="list" directory="j:\guides" recurse="false" name="nameofqry" type="file" sort="datelastmodified desc" filter="*.docx">

Next, perform a cfoutput using my alias as the a href link

<cfoutput query="nameofqry" maxrows="40">
    <li><a href="/guides/#nameofqry.name#" class="downloadlink" target="_blank">#nameofqry.name#</a></li>
</cfoutput>

Upvotes: 0

etchesketch
etchesketch

Reputation: 873

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.

Upvotes: 2

Related Questions