js二进制流文件导出

579 阅读1分钟
//根据条件导出活动报名用户基本信息导出名单
export function regUser(data) {
  return request({
    url: '/export/regUser',
    method: 'post',
    data,
    responseType: "arraybuffer"// 二进制流文件,一定要设置成blob,默认是json
  })
}
 // 名单导出
    MDexport() {
      regUser({
        keyword: this.suresearch.keyword
      }).then((res) => {
        let blob = new Blob([res], { type: "application/x-xls" });
        let link = document.createElement("a");
        link.href = window.URL.createObjectURL(blob);
        link.download = "名单导出.xlsx";
        link.click();
        // this.DownLoadFile(res, "导出");
        // console.log(res,'导出')
      });
    },
    
    //更新
    //接口
    //EXCEL导出
	execlExport(data) {
	  const options = {
	   responseType: 'arraybuffer' //重要
	  }
	 return request('execlExport', data, 'get', options)
	}
    //js文件
    function DownexportFiles(file, filename,type) {
        const blob = new Blob([file]);
          let fileName =''
            if(type==''){
              fileName = `${filename}`
            }else{
              fileName = `${filename}.${type}`
            }
      if ('download' in document.createElement('a')) { // 非IE下载
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);// 释放URL 对象
        document.body.removeChild(elink);
      } else { // IE10+下载
        navigator.msSaveBlob(blob, fileName);
      }
}
  export { DownexportFiles };
//引用
 import { DownexportFiles } from '@/methodFile/download'
  const download = (i) => {
      powerApi.downloadPdf({id:i.id}).then((res) => {
      DownexportFiles(res.data, i.superviseFirst, 'pdf')
 })
}