How to change URL.createObjectURL download name?
Asked Answered
L

1

12

I'm building an online csv converter that allows a user to upload a csv file and download the processed csv output file. Everything works fine, except the dowloaded file has a name that looks like "6fd665aa-74d7-4b4e-96e1-38aea0cca9e6.csv" (it changes every time) that has nothing to do with the input file's name.

How can I change this downloaded file name ?

const processedStr = convertCSV(text);
const myBlob = new Blob([processedStr], {type : 'text/csv'});
dllink.href = window.URL.createObjectURL(myBlob);
dllink.click();
Lacedaemon answered 19/12, 2018 at 7:20 Comment(0)
A
15

You can try to add an attribute to the dllink variable. It will give a name to the download attribute and hence the file.

const processedStr = convertCSV(text);
const myBlob = new Blob([processedStr], {type : 'text/csv'});
dllink.href = window.URL.createObjectURL(myBlob); 
dllink.setAttribute("download","custom_name.csv"); // Added Line 
dllink.click();
Afloat answered 19/12, 2018 at 7:22 Comment(3)
Perfect ! Thanks !Lacedaemon
If this works for you, please consider accepting the answer. I am glad I could help.Afloat
I totally will.Lacedaemon

© 2022 - 2024 — McMap. All rights reserved.