下载文件(后端反的二进制数据Bold,前端转成文件)

import Vue from 'vue'
import axios from 'axios'
Vue.prototype.$download = function download(url,param) {
  axios.get(url, {
    responseType: 'blob',
    params:param
  }).then((res) => {
    let blob = new Blob([res.data], {
      type: res.headers["content-type"]
    });
    var filename = decodeURI(res.headers["content-disposition"]).split(";")[1].split("=")[1];
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onload = (e) => {
      const a = document.createElement('a');
      a.download = filename;
      a.href = e.target.result;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  }).catch((err) => {
    console.log(err.message);
  })
}

  

原文地址:https://www.cnblogs.com/swt-axios/p/13384588.html