how to specify csv file name for downloading in [r

2020-06-20 12:41发布

I am exporting data using javascript to csv. for some reason i am not allowed to use the traditional <a download="filename.csv" /a> to set the file name.

I have the following line of code:

window.location.href = "data:text/csv;base64," + csvdata

Where and how can i insert and specify the file name and extension to make it work?

1条回答
Juvenile、少年°
2楼-- · 2020-06-20 13:18

It's not possible that way, try to emulate the <a href=.. with a click on it like this:

        var csvdata = "Hello World"; //  only for test
        var byteNumbers = new Uint8Array(csvdata.length);

		for (var i = 0; i < csvdata.length; i++)
		{
			byteNumbers[i] = csvdata.charCodeAt(i);
		}
		var blob = new Blob([byteNumbers], {type: "text/csv"});
   
        // Construct the uri
		var uri = URL.createObjectURL(blob);

		// Construct the <a> element
		var link = document.createElement("a");
		link.download = 'myfile.csv';
		link.href = uri;

		document.body.appendChild(link);
		link.click();

		// Cleanup the DOM
		document.body.removeChild(link);
		delete link;

查看更多
登录 后发表回答