vue xml文档流下载文件

export default {
methods: {

download(url, param) {
  let params = param
  let xhr = new XMLHttpRequest();
  xhr.open('POST', url, true);
  xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  xhr.responseType = 'blob';
  xhr.onload = function (e) {
    if (this.status == 200) {
      if (navigator.msSaveBlob) {
        return navigator.msSaveBlob(this.response, params.originalName ? params.originalName : '错误提示文档.xlsx')
      }
      let blob = this.response;
      let a = document.createElement('a');
      let url = window.URL.createObjectURL(blob);
      a.href = url;
      //获取后端文件名称
      let fileName = decodeURI(xhr.getResponseHeader('content-disposition'));
      //截取=字符串后面的内容
      let str = fileName.substring(21, fileName.length)
      let utfStr = decodeURI(escape(str))     //是ISO_8859_1格式->改成utf-8
      a.download = utfStr;
      a.click();
      window.URL.revokeObjectURL(url);
    }
  };
  //参数是json格式
  xhr.send(JSON.stringify(params));
}

}
}

你可能感兴趣的:(javascript,前端,vue.js,html,html5)