前端实现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);
        });
    },

  浏览器请求成功效果

 

posted @ 2021-05-20 15:22  zzzzzyyyyyy  阅读(948)  评论(0编辑  收藏  举报