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