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?
Using window. location. href it's not possible to send a POST request. What you have to do is to set up a form tag with data fields in it, set the action attribute of the form to the URL and the method attribute to POST, then call the submit method on the form tag.
Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.
The location. href property sets or returns the entire URL of the current page.
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;
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