Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  7] [ 3]  / answers: 1 / hits: 16515  / 12 Years ago, thu, november 15, 2012, 12:00:00

I want to export the exist data into csv file. I try to use this code:



var uriContent = data:text/csv;charset=utf-8, + encodeURIComponent(data);
var myWindow = window.open(uriContent);
myWindow.focus();


it works but I can design filename. I can only get the dialog with name like MVeAnkW8.csv.part which I don't know where the name come from.



How can I assign filename in the first dialog? Thanks in advance.



update:



I am now using rails. Actually I have a method in server side names export_to_csv. In end of this method, I use code like that:



  send_data(csv_string,
:type => 'text/csv; charset=utf-8;',
:filename => myfile.csv)


It works and I can specify the file name.
For now, I want to use ajax to get more csv files(that is the reason why I want to use javascript, because a normal http request can only get one file to be downloaded).



I use js code like that:



$.post(export_to_csv,
function(data) {
var uriContent = data:text/csv;charset=utf-8, + encodeURIComponent(data);
var myWindow = window.open(uriContent);
myWindow.focus();});


It get the data from server side and I try to transfer it into csv. I can get a file but can't specify the file name.


More From » csv

 Answers
63

As I know, you can specify the filename only in chrome 14+.
Take a look at this question: Is there any way to specify a suggested filename when using data: URI?



Update!



If you want to download multiple csv files at once, you can zip them together, or save each file on the server separately (each file now has a url that points to it - if you place them inside the 'www' folder).
Then send the file names and their path/url to the client via ajax (use a json encoded list for example).
In the ajax callback function: take the list of the files and open each file-url in a separate popup.


[#81970] Wednesday, November 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;