下载(导出),返回为bytes的字节流,先将bytes转换为blob,再下载

      //base64转blob
    b64toBlob(b64Data, contentType, sliceSize) {
      contentType = contentType || "";
      sliceSize = sliceSize || 512;
      let byteCharacters = atob(b64Data);
      let byteArrays = [];
      for (
        let offset = 0;
        offset < byteCharacters.length;
        offset += sliceSize
      ) {
        let slice = byteCharacters.slice(offset, offset + sliceSize);
        let byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) {
          byteNumbers[i] = slice.charCodeAt(i);
        }
        let byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
      }
      this.blob = new Blob(byteArrays, { type: contentType });
      // console.log(123, this.blob);
      // return blob;
    },
      this.b64toBlob(response.bytes);
           let blob = new Blob([this.blob], {
                  type: "application/vnd.ms-excel",
                });
                let fileName = response.fileName;
                let downloadElement = document.createElement("a");
                let href = window.URL.createObjectURL(blob); //创建下载的链接
                downloadElement.href = href;
                downloadElement.download = decodeURI(fileName); //下载后文件名
                document.body.appendChild(downloadElement);
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                window.URL.revokeObjectURL(href); //释放掉blob对象
      });
上一篇:下载附件时防止连点


下一篇:前端下载二进制流数据