前端下载txt/图片文件时,不弹窗下载,而是直接打开的问题

downloadFile(name, blob) { // 创建a标签 const link = document.createElement("a"); const url = URL.createObjectURL(blob); // download属性 link.setAttribute("download", name); // href链接 link.setAttribute("href", url); // 自执行点击事件 link.click(); document.body.removeChild(link); }, download(name, url) { // fetch抓取图片数据 fetch(url) .then(response => { if (response.status == 200) // 返回的.blob()为promise,然后生成了blob对象,此方法获得的blob对象包含了数据类型,十分方便 return response.blob(); throw new Error(status: ${response.status}.); }) .then(blob => { // 获取到blob对象 this.downloadFile(name, blob); }) .catch(error => { console.log("failed. cause:", error); }); },

posted @ 2020-07-06 11:35  廖伟名  阅读(1239)  评论(0编辑  收藏  举报