How to force download a pdf file to url?

I have a url that goes to a pdf file. On my coldfusion page, I want to allow the user to upload the file (using the open / save dialog or, nevertheless, this particular browser handles it).

This is the code that I still have:

<cfset tempFile = getTempFile(getTempDirectory(), 'testfile') /> <cfhttp url="myUrl/myFile.pdf" method="get" file="#tempFile#"/> <cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf"> <cfcontent type="application/pdf" file="#tempFile#"> 

This seems to work ... but when I try to open the file, it tells me something is wrong with the file. What am I doing wrong?

+8
source share
2 answers

file attribute: Do not specify the directory path in this attribute; use the path attribute.

Try separating the file name and path:

 <!--- hard coded for clarity ---> <cfhttp url="http://www.somesite.com/path/testFile.pdf" method="get" getAsBinary="yes" path="c:/test/" file="testFile.pdf"/> <cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" /> <cfcontent type="application/pdf" file="c:/test/testFile.pdf" /> 

For small files, you can skip the temp file and use <cfcontent variable..>

 <cfhttp url="http://download.macromedia.com/pub/documentation/en/coldfusion/mx7/cfmx7_cfml_qref.pdf" method="get" getAsBinary="yes" /> <cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" /> <cfcontent type="application/pdf" variable="#cfhttp.fileContent#" /> 

Update: Dynamic example using a temporary file

 <cfset tempDir = getTempDirectory() /> <cfset tempFile = getFileFromPath(getTempFile(tempDir, "testfile")) /> <!--- uncomment to verify paths <cfoutput> tempDir = #tempDir#<br /> tempFile = #tempFile#<br /> </cfoutput> <cfabort /> ---> <cfhttp url="http://download.macromedia.com/pub/documentation/en/coldfusion/mx7/cfmx7_cfml_qref.pdf" method="get" getAsBinary="yes" path="#tempDir#" file="#tempFile#" /> <cfheader name="Content-Disposition" value="attachment; filename=myFile.pdf" /> <cfcontent type="application/pdf" file="#tempDir#/#tempFile#" /> 
+12
source

As far as I know, your encoding in Google Chrome is beautiful. An error message appears in IE. This is because of the " file path " does not support the URL. Must use the directory path instead of the URL path.

0
source

All Articles