How to change URL.createObjectURL download name?

独自空忆成欢 提交于 2021-01-02 06:42:10

问题


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();

回答1:


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.href = window.URL.createObjectURL(myBlob);
dllink.setAttribute("download","custom_name.csv"); // Added Line 
dllink.click();


来源:https://stackoverflow.com/questions/53846343/how-to-change-url-createobjecturl-download-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!