我想强制浏览器下载pdf文件.
我使用以下代码:
<a href="../doc/quot.pdf" target=_blank>Click here to Download quotation</a>
Run Code Online (Sandbox Code Playgroud)
它使浏览器在新窗口中打开pdf,但我希望它在用户单击时下载到硬盘驱动器.
我发现它Content-disposition用于此,但我如何在我的情况下使用它?
我在我的htaccess文件中有这条规则强制链接文件下载而不是在浏览器中打开:
<FilesMatch "\.(gif|jpe?g|png)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)
有没有办法改变它RegExp所以它只适用于某个目录中的文件?
谢谢
对于知道答案的人来说,简单点.我的代码通过跨文档消息传递成功地从网站下载pdf.但是,我现在想在浏览器中显示pdf,可能在iframe或数据对象中.可能我需要知道pdf存储为已下载的本地URL.请帮忙,可能很容易点.请参阅此处的小提琴以获取我的代码.
重要提示:因为我想下载文件,我不想简单地制作client.src ="http://ops.epo.org/3.0/rest-services/published-data/images/US/7123345/B2/thumbnail .PDF?范围= 1"
HTML代码:
<input type="button" onclick="runit()" value="runit"></input>
<iframe width=100 height=100 id="client" src="http://ops.epo.org/3.0/xss/crosssitescript.html" />
Run Code Online (Sandbox Code Playgroud)
Javascript代码:
function runit() {
// Get the iframe window object
var client = document.getElementById('client');
// Create the data string to be passed to the OPS JavaScript
var data = "{'url' : 'http://ops.epo.org/3.0/rest-services/published-data/images/US/7123345/B2/thumbnail.pdf?Range=1', " + "'method' : 'GET', " + "'requestHeaders' : {'Accept': 'application/pdf' } " + "}";
// Use the postMessage() method in order to send the data to the
// iframe …Run Code Online (Sandbox Code Playgroud) .htaccess ×1
apache ×1
cross-domain ×1
download ×1
header ×1
html ×1
httpresponse ×1
javascript ×1
messaging ×1
pdf ×1