Skip to main content
Known Participant
April 29, 2010
Question

How do I force a file download from a folder above the root?

  • April 29, 2010
  • 1 reply
  • 9047 views

I am new to ColdFusion and need some help. I set up a virtual folder on my website and then mapped in the CF9 admin panel to a "Docs" folder above my root, where I would like to store sensitive documents. CF tags are enabled.

I know that my mapping is set up correctly, because when I set up a cfm page with the following code, I can successfully download the file:

<cfheader name="Content-Disposition" value="attachment; filename=Calendar.pdf">
<cfcontent type="application/pdf file=/Docs/Calendar.pdf">

But what I need is to create one cfm age that will be able to handle downloads of multiple files types and names from the folder above my root.

I then found Duke Snyder's solution on an older forum for the "click link and download file" question where he suggests making a "download.cfm" page with the following code:  I did this, and named the file "download.cfm"

<cfsetting enablecfoutputonly="yes">
<cfheader name="Content-disposition" value="attachment; filename=""#Url.FileName#""">
<cfcontent type = "foo/bar" file = "/Docs/#Url.FileName#">

Then Duke suggests: "Now I pass the FileName through the Url and it WILL ask if you want to open or download the file. We accomplish this by making up an eronious file type."

I then set up a password-protected page on the site that lists the titles of a number of documents, with links to the virtual folder, where they can be downloaded.  One of the links, which goes to "Calendar.pdf" I constructed as: "download.cfm?FileName=Calendar.pdf"  When I click on the link, I get the popup box that says "Do you want to open or save this file?  With the name listed as: "download.cfm?Filename=Calendar_pdf"

When I click "Save", I get the message: "Internet Explorer cannot download download.cfm from www.... Internet Explorer was not able to open this Internet site.  The requested site is either unavailable or cannot be found. Please try again later.

I must have constructed the link incorrectly. Any ideas?  Thanks in advance for your help.

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
April 30, 2010

I then found Duke Snyder's solution on an older forum for the "click link and download file" question where he suggests making a "download.cfm" page with the following code:  I did this, and named the file "download.cfm"

<cfsetting enablecfoutputonly="yes">
<cfheader name="Content-disposition" value="attachment; filename=""#Url.FileName#""">
<cfcontent type = "foo/bar" file = "/Docs/#Url.FileName#">

Then Duke suggests: "Now I pass the FileName through the Url and it WILL ask if you want to open or download the file. We accomplish this by making up an eronious file type."

I then set up a password-protected page on the site that lists the titles of a number of documents, with links to the virtual folder, where they can be downloaded.  One of the links, which goes to "Calendar.pdf" I constructed as: "download.cfm?FileName=Calendar.pdf"  When I click on the link, I get the popup box that says "Do you want to open or save this file?  With the name listed as: "download.cfm?Filename=Calendar_pdf"

This should work:

download.cfm

============

<cfsetting enablecfoutputonly="yes">
<cfheader name="Content-disposition" value="attachment; filename=""#Url.FileName#""">
<cfcontent type = "application/pdf" file = "/Docs/#Url.FileName#">

Then browse to the URL containing download.cfm?filename=Calender.pdf

Known Participant
May 3, 2010

BKBK,

Thanks so much for your help.

BKBK
Community Expert
Community Expert
May 19, 2010

Hi BKBK,

When I saw your first email answer, I clicked the question as "Answered" before I had time to test the function.

Then, when I tested it, I was not successful.

I just got back to my office yesterday afternoon, and will test your second option tonight. I'll let you know how it goes. Like I said in my original thread, I am totally new to ColdFusion and am generally clueless about the tags.

Sorry for the confusion - And I greatly appreciate your taking the time to offer a second option!

I'll let you know if it works!

ScooterBoogle


I inderstand.