javascript DIV onClick 事件强制下载pdf文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18505692/
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
DIV onClick event force download a pdf file
提问by marcin
I want to make a DIV onclick which will open a dialog box to save a pdf file.
我想制作一个 DIV onclick,它将打开一个对话框来保存 pdf 文件。
Something like this (html5 a href):
像这样的东西(html5 a href):
<a href="path" download>download</a>
but using onclick event on DIV.
但在 DIV 上使用 onclick 事件。
i tried but it's not working:
我试过了,但它不起作用:
<div onClick="window.location='path' download">something</div>
回答by Micha? Prajsnar
<div onClick="window.location=location/to/file/pdf.php">something</div>
pdf.php:
pdf.php:
<?php
header('Content-disposition: attachment; filename=your_file.pdf');
header('Content-type: application/pdf');
readfile('path/to/your_file.pdf');
回答by karaxuna
Try:
尝试:
<div onClick="window.location='path'; return false;">something</div>
You should set correct headers on server side which will determine view or download a file
您应该在服务器端设置正确的标头,这将决定查看或下载文件
回答by decongh
The header sections of this kind of apps must be taking seriously if you want the to work, if you do not have those show by Micha? Prajsnar, i say go back and put them in and try again.
如果您想要工作,此类应用程序的标题部分必须认真对待,如果您没有 Micha 的节目?Prajsnar,我说回去把它们放进去再试一次。
You can just google that same thing and solutions on the fly.
你可以在谷歌上搜索同样的事情和解决方案。
try these :
试试这些:
http://www.amember.com/forum/threads/force-file-downloads-with-htaccess.14980/
http://www.amember.com/forum/threads/force-file-downloads-with-htaccess.14980/
回答by Travis Clarke
Try:
尝试:
<div onClick="window.open('path', '_blank', 'fullscreen=yes'); return false;">something</div>
This works fine for me.
这对我来说很好用。