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);
});

你可能感兴趣的:(axios下载blob文件)