vue中使用文件流进行下载(new Blob)

tech2025-02-28  5

this.$axios .post(url接口地址, params请求参数, { headers: { token: token }, responseType: "arraybuffer" }) .then((file) => { let content = file.data; // 组装a标签 let elink = document.createElement("a"); // 设置下载文件名 elink.download = "附件.zip"; elink.style.display = "none"; let blob = new Blob([content], {type: "application/zip"}) elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); document.body.removeChild(elink); })

注意:responseType应设置为:'arraybuffer',这样返回的文件流才会是二进制的,才能使用new Blob得到正确的文件

最新回复(0)