DIV onClick event force download a pdf file

夙愿已清 提交于 2021-02-08 09:29:36

问题


I want to make a DIV onclick which will open a dialog box to save a pdf file.

Something like this (html5 a href):

<a href="path" download>download</a>

but using onclick event on DIV.

i tried but it's not working:

<div onClick="window.location='path' download">something</div>

回答1:


<div onClick="window.location=location/to/file/pdf.php">something</div>

pdf.php:

<?php
header('Content-disposition: attachment; filename=your_file.pdf');
header('Content-type: application/pdf');
readfile('path/to/your_file.pdf');



回答2:


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




回答3:


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.

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://webdesign.about.com/od/php/ht/force_download.htm




回答4:


Try:

<div onClick="window.open('path', '_blank', 'fullscreen=yes'); return false;">something</div>

This works fine for me.



来源:https://stackoverflow.com/questions/18505692/div-onclick-event-force-download-a-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!