写下载的时候,axios的responseType: "blob"(一定要修改)

download(data, fileName) {
      // window.URL.createObjectURL创建一个地址。
      const url = window.URL.createObjectURL(new Blob([data])); // URL.createObjectURL(data);
      // 前端a标签可以转化为下载模式,需要在有href的状态下,添加download属性,然后触发a标签
      const a = document.createElement('a');
      a.href = url;
      a.setAttribute('download', fileName);
      document.body.appendChild(a).click();
      // 触发下载以后,需要清空window.URL.createObjectURL创建的地址,解除内存占用
      a.parentNode.removeChild(a);
      URL.revokeObjectURL(url);
    },
posted @ 2021-12-01 20:17  大大的可爱  阅读(149)  评论(0编辑  收藏  举报