• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Apr 29, 2010 Apr 29, 2010

Copy link to clipboard

Copied

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.

TOPICS
Server administration

Views

8.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 30, 2010 Apr 30, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 03, 2010 May 03, 2010

Copy link to clipboard

Copied

BKBK,

Thanks so much for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 13, 2010 May 13, 2010

Copy link to clipboard

Copied

I am trying to force the download of pdf files from a folder above the root.  The file below is named "download.cfm"  I pass the name of the file (Calendar.pdf) in the URL link, as follows: download.cfm?filename=Calender.pdf

In the ColdFusion admin panel, I mapped a folder in the root "Docs"  to point to a folder with a different name above the root.   I have tried the code below, which was generously supplied by another member of this forum, but it does not seem to recognize the CF mapping. Do I need to use another CF Tag for the mapping to working correctly?

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

Thanks for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 13, 2010 May 13, 2010

Copy link to clipboard

Copied

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#')#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2010 May 18, 2010

Copy link to clipboard

Copied

Hi BKBK,

Once again, I thank you for your suggestions. I'll give it a try.

Thanks!

ScooterBoogle

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2010 May 18, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2010 May 18, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 19, 2010 May 19, 2010

Copy link to clipboard

Copied

I inderstand.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

@Acainion

Could you show us your code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

<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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

Acainion wrote:

cfheader only wanted the file name.

cfcontent needed the full path.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2013 Apr 26, 2013

Copy link to clipboard

Copied

LATEST

Thanks a bunch for helping out.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation