[英]How can I force a download of a pdf in a url?
I have a URL that goes to a pdf file. 我有一个转到pdf文件的URL。 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). 在我的Coldfusion页面中,我希望允许用户下载文件(使用“打开/保存”对话框或由特定的浏览器处理)。
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. 使用path属性。
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..>
对于较小的文件,您可以跳过临时文件,并使用<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#" />
As far as I know, your coding is fine in Google Chrome. 据我所知,您的编码在Google Chrome中是可以的。 In IE, error message prompt. 在IE中,错误消息提示。 It's because of " file path " cannot support for URL path. 这是因为“ 文件路径 ”无法支持URL路径。 Should use directory path instead of URL path. 应该使用目录路径而不是URL路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.