I am using Javascript to create a CSV file for user to download.
Until May 22nd, Chrome still downloaded the file with the name I specified. However, today I found that the files downloaded are named "download" and do not have the extension .csv.
This problem does not exist in Firefox!
Here is a fiddle with sample Javascript:
var A = [['n','sqrt(n)']]; // initialize array of rows with header row as 1st item
for(var j=1;j<10;++j){ A.push([j, Math.sqrt(j)]) }
var csvRows = [];
for(var i=0,l=A.length; i<l; ++i){
csvRows.push(A[i].join(',')); // unquoted CSV row
}
var csvString = csvRows.join("\n");
var a = document.createElement('a');
a.href = 'data:text/csv;charset=utf-8;base64,' + window.btoa(csvString);
a.target = '_blank';
a.download = 'myFile.csv';
document.body.appendChild(a);
a.click();
To trigger a file download on a button click we will use a custom function or HTML 5 download attribute. The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded.
Nice work! This is a regression.
I just created another fiddle, and filed a Chrome bug.
If you're interested, star it in the bug tracker.
<a href="/" download="my-downloaded-file.html" target="_blank">Click here</a>
EDIT: It look like it depends on the URL. Absolute URLs work, as well as objects URLs (according to https://code.google.com/p/chromium/issues/detail?id=376197).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With