js導出文件的兩種方式


              //創建a標簽防止被瀏覽器攔截
              var a = document.createElement("a");
              a.setAttribute(
                "href",
                "/static/file/export/" + response.data.data
              );
              a.setAttribute("target", "_blank");
              a.click();            

模擬a標簽點擊,會打開一個新的頁面一閃而過獲取文件。指向文件地址。

 const filename = response.headers["content-disposition"]
                .split("filename=")[1]
                .split("; filename")[0];
              const url = window.URL.createObjectURL(response.data);
              const link = document.createElement("a");
              link.style.display = "none";
              link.href = url;
              link.setAttribute("download", filename);
              document.body.appendChild(link);
              link.click();

獲取到文件流信息,前端處理獲取文件,不會打開新的頁面。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM