save file onclick

浪子不回头ぞ 提交于 2019-12-10 13:34:16

问题


I want to be able to download the csv file on click and not open in the browser

I put this code

<a href="file.csv">download file</a>

but on click it opens thev file in the browser. In localhost, when I click the link it is being downloaded but when on the server it's opening in the browser


回答1:


You have to set headers in order to inform web-browser for opening file-save dialog.

download.php


  <?php
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=sample.csv");
    readfile('file.csv');
   ?>

showlinks.php


<a href="download.php">Download file</a>



回答2:


Try Using following JS Code:

<a href="javascript:void(0);" onclick="javascript:document.execCommand('SaveAs','1','file.csv')>download file</a>



回答3:


HTML5 gives you another way - download attribute. The problem is currently only Chrome supports it. It will take some time till it will become widely used solution.

So for now I would recommend you @AVD's PHP code.



来源:https://stackoverflow.com/questions/11664402/save-file-onclick

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