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


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得到正确的文件


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM