前端實現txt文件下載-后端傳文件流,前端下載


<a class="abAccount" @click="downLoadTxt(scope.row.taskId, scope.row.taskName)">采集異常賬號下載</a>

  

    // 文件下載
    downLoadTxt(taskId, taskName) {
      let _this = this;
      this.$axios({
        withCredentials: false,
        Accept: "application/json, text/plain, */*",
        url: this.taskNewUrl + "admin/taskManage/downLoad",
        // responseType: "blob",
        method: "post",
        data: {
          data: {
            taskId: taskId,
          },
        },
      })
        .then(function (res) {
          const { data } = res;
          const blob = new Blob([data]);
          // let disposition = decodeURI(res.headers["content-disposition"]);
          // 從響應頭中獲取文件名稱
          let fileName = taskName;
          if ("download" in document.createElement("a")) {
            // 非IE下載
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 釋放URL 對象
            document.body.removeChild(elink);
          } else {
            // IE10+下載
            navigator.msSaveBlob(blob, fileName);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

  瀏覽器請求成功效果

 


免責聲明!

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



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