download file ...">

Save onclick file

I want to download the csv file by click and not open in the browser

I put this code

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

but when clicked, it opens thev file in browser. In localhost, when I click the link, it loads, but when it opens on the server in the browser

+6
source share
3 answers

You must set headers to tell the web browser to open the save file dialog box.

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> 
+11
source

Try using the following JS code:

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

HTML5 gives you another way - download . Currently, the problem is only in Chrome. It will take some time until it becomes a widely used solution.

So now I would recommend you @AVD PHP code.

+1
source

Source: https://habr.com/ru/post/921371/


All Articles