ant design 文件下载

	文件下载
	//导出报表
const exportExel = () => {
    axios({
      method: 'post',
      url: '/insMngGjzy/comb/attribution/export',
      responseType: 'arraybuffer',
      data: {
        combId: Number(id),
        type: tempOthers?.type || 4,
        startDate: tempOthers?.dateArr?.[0] || '',
        endDate: tempOthers?.dateArr?.[1] || '',
        industryCode: tempOthers?.industryCode || infoObj?.hydm || 0
      },
      headers: {
        clientType: 1,
        accessToken:
          getCookie('token') ||
          'DC0956D491D5BCA4B86809D38DD5C5CB2CA3350B6BE7D98DEEF4EBDDF721A92735FE7A58BA18AE7B6EBDACEEFE27DFF845D0158C216718F3'
      }
    }).then((res: any) => {
      getFile(
        res,
        `${infoObj.zzmc}-${infoObj.zhmc}业绩归因报表`,
        'xlsx',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      );
    });
  };
  export const getFile = (res: any, title: string, suffix: string, type: string) => {
    const blob = new Blob([res.data], { type });
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onload = e => {
      const a = document.createElement('a');
      a.download = `${title}.${suffix || 'xls'}`;
      a.href = e.target.result;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    };
  };
  //下载文件
  const getUrl = () => {
    dispatch({
      type: 'combManage/goGetSysUrl',
      payload: {
        type: 'STOCK_POOL_IMPORT_MODEL'
      },
      callback: (res: any) => {
        if (res.code === 1008) {
          downLoadFromUrl(res.data.url);
        }
      }
    });
  };
  export const downLoadFromUrl = (url: string) => {
    if (url) {
      var x = new XMLHttpRequest();
      x.open('GET', url, true);
      x.responseType = 'blob';
      x.onload = function(e) {
        download(x.response, url.substr(url.lastIndexOf('/') + 1), 'application/vnd.ms-excel');
      };
      x.send();
    }
  };
  //下载图片
  const getQqrCode=()=>{
    axios({
        method: 'post',
        url: '/InvestmentAssistant/qrCode/image.do',
        responseType: 'arraybuffer',
        data: {
          type:1
        },
        headers: {
          clientType: 1,
        }
      }).then((res: any) => {
        var data = res.data;
    let imageUrl =
      "data:image/png;base64," +
      btoa(
        new Uint8Array(data).reduce(
          (data, byte) => data + String.fromCharCode(byte),
          ""
        )
      );
      setCode(imageUrl)
      },()=>{setCode("")});
   
}
// 获取研报并且跳转
export const getReport = (
  id: number | string,
  flag: string,
  reportTitle: string,
  callback: () => {}
) => {
  axios({
    method: 'get',
    url: `/insMngGjzy/report/${flag}/${id}`,
    responseType: 'arraybuffer',
    headers: {
      clientType: 1,
      accessToken:
        getCookie('token') ||
        'DC0956D491D5BCA4B86809D38DD5C5CB2CA3350B6BE7D98DEEF4EBDDF721A92735FE7A58BA18AE7B6EBDACEEFE27DFF845D0158C216718F3'
    }
  }).then((res: any) => {
    console.log('resres', res);
    if (res?.status === 200) {
      const blob = new Blob([res.data], { type: '' });
      const reader = new FileReader();
      reader.readAsDataURL(blob);

      reader.onload = e => {
        if (e.target.result) {
          //docx类型
          if (res.headers['content-disposition']) {
            if (res.headers['content-disposition'].indexOf('docx') > -1) {
              let disposition: { [key: string]: any } = { filename: reportTitle + '.docx' };
              const headersContent = res.headers['content-disposition'].split(';');
              headersContent.forEach((item: string) => {
                const itemArr = item.split('=');
                if (itemArr.length === 2) {
                  disposition[itemArr[0].trim()] = itemArr[1];
                }
              });
              // 文件流
              const a = document.createElement('a');
              a.download = decodeURIComponent(disposition['filename']); //对中文进行解码decodeURIComponent方法
              a.href = e.target.result;
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
              callback();
              return;
            }
          }
          //pdf类型
          localStorage.setItem('pdfCtt', e.target.result);
          window.open(`${window.location.origin}/institute_gjzy/pdf?id=${id}`, '_blank');
          callback();
        }
      };
    }
  });
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值