如何更改URL.createObjectURL下载名称? [英] How to change URL.createObjectURL download name?

查看:368
本文介绍了如何更改URL.createObjectURL下载名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在线csv转换器,允许用户上载csv文件并下载处理后的csv输出文件.一切正常,只是下载的文件的名称看起来像"6fd665aa-74d7-4b4e-96e1-38aea0cca9e6.csv"(每次都会更改)与输入文件的名称无关.

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

推荐答案

您可以尝试向 dllink 变量添加属性.它将为 download 属性命名,并为文件命名.

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

这篇关于如何更改URL.createObjectURL下载名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆