Javascript 提交表单时自动“下载”PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10901139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatic "download" PDF as upon form submission
提问by James Gu
I have a form button, when clicked it submits the form.
我有一个表单按钮,点击它提交表单。
I'd like that at the same time, the browser starts downloading a PDF file. I wonder how can I do this? Please keep in mind that PDF are usually opened by the browser by default, but I'd like the browser to "Save As" the file and not open the file. Is this do-able using html only or do I need javascript?
我想同时,浏览器开始下载PDF文件。我想知道我该怎么做?请记住,默认情况下,PDF 通常由浏览器打开,但我希望浏览器“另存为”文件而不打开文件。这是否可以仅使用 html 或我需要 javascript?
回答by kiranvj
If you are using PHP in the server side try this. This is tested code which is running in one of my websites.
如果你在服务器端使用 PHP,试试这个。这是在我的网站之一中运行的经过测试的代码。
<?php
ob_start();
$file = 'YOUR-FILENAME-HERE.pdf';
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
?>
Browser will prompt to download without displaying it in the browser.
浏览器会提示下载而不在浏览器中显示。
回答by Nishu Tayal
You can use this pluginto download the files using javascript.
Otherwise in javascript, you can write code for it.
您可以使用此插件使用 javascript 下载文件。
否则在 javascript 中,您可以为它编写代码。
$('a').click(function(e) {
e.preventDefault(); //stop the browser from following
window.location.href = 'downloads/file.pdf';
});
<a href="no-script.html">Download now!</a>
Or, you can use this .
或者,您可以使用此 .
In PHP :
在 PHP 中:
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
readfile("file.pdf");
?>
In Javascript :
在 JavaScript 中:
<body>
<script>
function downloadme(x){
winObj = window.open(x,'','left=10000,screenX=10000');
winObj.document.execCommand('SaveAs','null','download.pdf');
winObj.close();
}
</script>
<a href=javascript:downloadme('file.pdf');>Download this pdf</a>
</body>
回答by Caio Bianchi
In that case, you'd have to configure your webserver to force a PDF to Download. If you're using Apache, you might as well take a look at this article -> http://www.thingy-ma-jig.co.uk/comment/7264
在这种情况下,您必须配置您的网络服务器以强制下载 PDF。如果你使用 Apache,你不妨看看这篇文章 -> http://www.thingy-ma-jig.co.uk/comment/7264
Or, if you don't have to support all browsers, you could get away with just using the html5 download attribute -> http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
或者,如果您不必支持所有浏览器,您可以只使用 html5 下载属性 -> http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-下载
回答by iddo
Option 1: You can let the url, which your form submits to, return the download.
选项 1:您可以让表单提交到的 url 返回下载。
Option 2: Use an ajax call to submit the data onclick, set the window.location to your download url.
选项 2:使用 ajax 调用提交数据 onclick,将 window.location 设置为您的下载 url。
Whether the pdf is opened in the browser or downloaded, is a client side setting which you cannot control.
无论是在浏览器中打开还是下载pdf,都是您无法控制的客户端设置。