Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a download of a pdf in a url?

I have a URL that goes to a pdf file. In my coldfusion page, I want to allow the user to download the file (using the open/save dialog or however that particular browser handles it).

This is the code I have so far:

<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's wrong with the file. What am I doing wrong?

like image 787
froadie Avatar asked Jul 26 '11 19:07

froadie


People also ask

Can a website force a download?

Websites you visit can download and install software without your knowledge or approval. This is called a drive-by download. The objective is usually to install malware, which may: Record what you type and what sites you visit.

How do I trigger a download in HTML?

You need to utilize the <input> tag to create an HTML button and set its value to “Download”. The value is a user-defined attribute and can be set as per the user's wish. The script executes in the test. html file.

How do I make a PDF download automatically?

To Make Google Chrome Download PDF Files Instead of OpeningOn the right, go to the Content section, and click on Additional content settings. Click on PDF documents. On the next page, turn on (enable) the Download PDF files instead of automatically opening them in Chrome option. You are done.


1 Answers

file attribute: Do not specify the path to the directory 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 smaller files you might 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 temp 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#" />
like image 94
Leigh Avatar answered Sep 24 '22 20:09

Leigh