简体   繁体   中英

Download php genereted csv file though jquery/javascript

I can download a csv file from php, but I need to do it via jquery/javascript.

what would be the best solution for that? how to do it?

  • Save php generated csv file in tmp dir and download via jquery/JS but how?(app used internally)
  • Send array of data back to frontend, do all in jquery/JS.

Actually I want to update data on page and same time download changed rows! I can not use any plug-in of any kind.

EDIT: Ok! HERE is the full problem, i forget to mention.

I have a page with some rows from DB,

  • I wanted to update selected rows in DB
  • Same time get changed values and update rows on page
  • download changed rows in csv

In one button click,

I cant use different buttons to save data and download file because after updating rows in db how would I get last updated rows? With my limited knowledge, I can just get json response and update page Or download file not both!

So here is how i solved it :)

I made csv string out of json returned data in JS. and used data URI scheme like here: I made a link and that downloads .csv file.

var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(csv_ouput);
$('#csv-btn-div').append('<a href="' + uri + '"download="csv-export.csv">Download CSV</a>');

There is another solution , if you want file save dialog to appear!

Thanks All!

$.get("/someCsv.php", function(data) {
  // do stuff here.
});

Make sure to use some form of caching at the server side.

I would prefer preprocessed data (json):

$.get("/createJson.php", function(data) {
  // do stuff here.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM