问题
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