vue後臺傳文件流blob對象,前臺點擊下載

this.$axios

 .get("/wx/villageargot/export", {

 params: {

 subDistrictId: this.query.streetId

 },

 responseType: "blob"

 })

 .then(res => {
     const link = document.createElement("a");
     let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
     link.style.display = "none";
     link.href = URL.createObjectURL(blob);
     let num = "";
     for (let i = 0; i < 10; i++) {
        num += Math.ceil(Math.random() * 10);
     }
     link.setAttribute("download", "用戶_" + num + ".xls");
     document.body.appendChild(link);
     link.click();
     // 直接刪除了a標籤 無需經過 window.URL.revokeObjectURL(link.href)釋放內存
     document.body.removeChild(link);
     NProgress.done();
 })
 .catch(error => {
 this.$Notice.error({ title: "錯誤", desc: "網絡鏈接錯誤" });
 });

前端js/vue下載後臺傳過來的流文件(如excel)並設置下載文件名前端

相關文章
相關標籤/搜索