Cannot Specify Download File Name Using Javascript

I am using Javascript to create a CSV file for download.

Until May 22, Chrome still uploaded the file with the specified name. However, today I discovered that the downloaded files are called “downloads” and do not have the .csv extension.

This problem does not exist in Firefox!

Here is a fiddle with a Javascript sample:

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(); 
+7
javascript jquery html google-chrome csv
source share
1 answer

Good job! This is a regression.

I just created another fiddle and filed a Chrome Error .

If you're interested, run it in the error tracker.

 <a href="/" download="my-downloaded-file.html" target="_blank">Click here</a> 

EDIT: It looks like it depends on the url. Absolute URLs as well as URLs of objects (according to https://code.google.com/p/chromium/issues/detail?id=376197 ).

+6
source share

All Articles