Download php genereted csv file though jquery/javascript

江枫思渺然 提交于 2019-12-12 02:46:19

问题


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!


回答1:


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!




回答2:


$.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.
});


来源:https://stackoverflow.com/questions/19771147/download-php-genereted-csv-file-though-jquery-javascript

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