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

12,731

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#" />
Share:
12,731

Related videos on Youtube

froadie
Author by

froadie

Updated on June 04, 2022

Comments

  • froadie
    froadie almost 2 years

    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?

    • froadie
      froadie
      @leigh - method="getAsBinary" throws an error... did you mean getAsBinary="yes"?
  • Leigh
    Leigh over 12 years
    What do you mean by directory path and where?
  • froadie
    froadie over 12 years
    Thanks! Separating the file name and path didn't help, but the second suggestion (using the variable) worked!
  • Leigh
    Leigh over 12 years
    That is surprising. Both snippets worked for me under CF9. Just curious, what code did you use to separate the path?
  • froadie
    froadie over 12 years
    I'm using CF8. I saved the getTempDirectory() into a variable instead of using it on the fly to get the temp file, so I had the path, and I separated the file name using something like listLast(tempFile, '/')
  • Leigh
    Leigh over 12 years
    @froadie - Works perfectly for me. I do not think it is a version problem. Double check your variables. Make sure they are what you think they are ;) See update.