如何强制在 url 中下载 pdf?

编程入门 行业动态 更新时间:2024-10-27 00:29:17
本文介绍了如何强制在 url 中下载 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个指向 pdf 文件的 URL.在我的coldfusion页面中,我想允许用户下载文件(使用打开/保存对话框或特定浏览器处理它).

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).

这是我目前的代码:

<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="www.somesite/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" />

对于较小的文件,您可以跳过临时文件并使用 <cfcontent variable..>

For smaller files you might skip the temp file and use <cfcontent variable..>

<cfhttp url="download.macromedia/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="download.macromedia/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?

本文发布于:2023-11-01 20:40:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:url   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!