Skip navigation
scooterboogle
Currently Being Moderated

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

Apr 29, 2010 11:25 AM

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.

 
Replies
  • Currently Being Moderated
    Apr 30, 2010 3:41 AM   in reply to scooterboogle

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 13, 2010 10:51 PM   in reply to scooterboogle

    Then try this instead

     

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

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2010 2:06 PM   in reply to scooterboogle

    I am curious why you mark the question as already answered. Whereas you suggest you're still looking for the answer.

     
    |
    Mark as:
  • Currently Being Moderated
    May 19, 2010 4:40 AM   in reply to scooterboogle

    I inderstand.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 10:36 AM   in reply to BKBK

    Hi, I'm using the same code similar to scooterboogle and I have the option to save or open but it opens as a .cfm but I want to open as the file type. What am I missing?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 10:46 AM   in reply to Acainion

    @Acainion

    Could you show us your code?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 10:53 AM   in reply to BKBK

    <br>
    <a href="download.cfm?filename=22_Test.txt" target="_blank">Download 22_Test.txt</a>

     

    --- download.cfm
    <cfparam name="url.filename" default="">
    <cfset myPath = "#application.system_common#\docstorfilemove\workers_comp_forms\22\">
    <cfset fileAndPath = myPath & url.filename>


    <cfif (url.filename NEQ "") AND (fileExists(fileAndPath))>


    <cfheader name="content-disposition" value="attachment:filename=#url.filename#">
    <cfcontent file="#fileAndPath#" type="text/plain" deletefile="no">

    <cfelse>
    <p><B>Error:</B> No filename sent or file does not exist</p>
    </cfif>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 11:32 AM   in reply to Acainion

    It all depends on what the path #application.system_common# resolves to. If it is up to and including the current directory, then you could do something like

     

    <cfset myFullPath = expandpath("#application.system_common#\docstorfilemove\workers_comp_ forms\22\")>

     

    <cfif isDefined("URL.filename") and fileExists(fileAbsolutePath)>

    <cfset fileAbsolutePath = myFullPath & url.filename>

    <cfheader name="content-disposition" value="attachment:filename=#url.filename#">

    <cfcontent file="#fileAbsolutePath#" type="text/plain" deletefile="no">

     

    <cfelse>

    <p><B>Error:</B> No filename sent or file does not exist</p>

    </cfif>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 12:15 PM   in reply to BKBK

    I still have the same issue but the download seems to work better without the expandPath.... coming from a directory. The following change was made <cfheader name="content-disposition" value="attachment; filename=#listlast(fileAbsolutePath,'\,/')#"> ...... I was giving the full path instead of the actual filename with extension.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 12:16 PM   in reply to Acainion

    What is the relation between the directory application.system_common, the web root and the current directory?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 12:20 PM   in reply to BKBK

    the application.system_common was giving the full path to the directory folder, which cfheader didn't want. cfheader only wanted the file name. cfcontent needed the full path. I have tested and it works now.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 12:25 PM   in reply to Acainion

    Acainion wrote:

     

    cfheader only wanted the file name.

    cfcontent needed the full path.

    That, in fact, is the idea in a nutshell!

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 26, 2013 12:30 PM   in reply to BKBK

    Thanks a bunch for helping out.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points