如何在url中强制下载pdf? [英] How can I force a download of a pdf in a url?
问题描述
这是代码我到目前为止:
< cfset tempFile = getTempFile(getTempDirectory(),'testfile')/>
< cfhttp url =myUrl / myFile.pdfmethod =getfile =#tempFile#/>
< cfheader name =Content-Dispositionvalue =attachment; filename = myFile.pdf>
< cfcontent type =application / pdffile =#tempFile#>
这似乎工作...但是当我尝试打开文件时,会告诉我有什么问题文件。我做错了什么?
文件属性:不要在此属性中的目录中指定路径;使用
路径属性。
尝试分离文件名和路径:
<!---硬编码为清楚--->
< cfhttp url =http://www.somesite.com/path/testFile.pdf
method =get
getAsBinary =yes
path = c:/ test /
file =testFile.pdf/>
< cfheader name =Content-Dispositionvalue =attachment; filename = myFile.pdf/>
< cfcontent type =application / pdffile =c:/test/testFile.pdf/>
对于较小的文件,您可能会跳过临时文件,并使用< cfcontent变量..>
< cfhttp url =http://download.macromedia.com /pub/documentation/en/coldfusion/mx7/cfmx7_cfml_qref.pdf
method =get
getAsBinary =yes/>
< cfheader name =Content-Dispositionvalue =attachment; filename = myFile.pdf/>
< cfcontent type =application / pdfvariable =#cfhttp.fileContent#/>
更新:动态使用临时文件的示例
< cfset tempDir = getTempDirectory()/>
< cfset tempFile = getFileFromPath(getTempFile(tempDir,testfile))/>
<!---取消注释以验证路径
< 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-Dispositionvalue =attachment; filename = myFile.pdf/>
< cfcontent type =application / pdffile =#tempDir#/#tempFile#/>
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?
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#" />
这篇关于如何在url中强制下载pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!