axios下载blob文件

this.$axios({
  method: 'get',
  url: '/task-common-api/api/v1/task/whitelist/template',
  responseType: "blob"
})
.then(function (response) {
  console.log(response);
  if(response.data.type == "application/octet-stream") {
    const filename = response.headers["content-disposition"];
    const blob = new Blob([response.data]);
    var downloadElement = document.createElement("a");
    var href = window.URL.createObjectURL(blob);
    downloadElement.href = href;
    downloadElement.download = filename.split("filename=")[1];
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(href);
  }
})
.catch(function (error) {
  console.log(error);
});
posted @ 2022-03-08 09:13  _Miao  阅读(220)  评论(0编辑  收藏  举报