5
let url = '/FileService/Export/Task/download', param;

 axios.post(url, param, {responseType: 'blob'})
      .then(function(response) {
        let res = response.data;
        if (res.type.indexOf('application/json') !== -1) {
          let reader = new FileReader();
          reader.readAsText(res, 'utf-8');
          reader.onload = function (e) {
            let data = JSON.parse(reader.result);   //e.target.result也可
            if(data.info){
              message.error(data.info);
            }else if(data.msg){
              message.error(data.msg);
            }
          }
        }
        else{
          let blob = new Blob([response.data]);
          let downloadElement = document.createElement('a');
          let href = window.URL.createObjectURL(blob); //创建下载的链接
          let fileName = response.headers["content-disposition"].split(";")[1].split("filename=")[1].split(".zip")[0];
          if (isIE()) {
            window.navigator.msSaveBlob(blob, fileName);
          } else {
            downloadElement.href = href;
            downloadElement.download = `${fileName}.zip`; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
        }
      })
      .catch(function(error) {
        console.error("error",error)
        message.error("出现异常,请重试");
      });

machinist
460 声望33 粉丝

javaScript、typescript、 react全家桶、vue全家桶、 echarts、node、webpack