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

后端 未结 2 2086
[愿得一人]
[愿得一人] 2020-12-17 22:51

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 han

相关标签:
2条回答
  • 2020-12-17 23:38

    As far as I know, your coding is fine in Google Chrome. In IE, error message prompt. It's because of "file path" cannot support for URL path. Should use directory path instead of URL path.

    0 讨论(0)
  • 2020-12-17 23:50

    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#" />
    
    0 讨论(0)
提交回复
热议问题