前端常见下载功能

1、二进制文件下载,服务端提供二进制文件 BLOB

 const onExport = (startTime, endTime, type) => {
    axios
      .post(
        '/api/customer/manage/exportClueUser',
        {
          startTime,
          endTime,
          type,
        },
        {
          headers: {
            Accept: 'application/json',
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
          },
          responseType: 'blob',
        },
      )
      .then((response) => {
        const {
          data,
          headers: { filename },
          status,
        } = response;
        if (status === 200) {
          let a = document.createElement('a');
          a.download = filename;
          a.style.display = 'none';
          let blob = new Blob([data], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          });
          a.href = URL.createObjectURL(blob);
          document.body.appendChild(a);
          a.dispatchEvent(new MouseEvent('click'));
          document.body.removeChild(a);
        }
      });
  };

2、服务端提供下载地址

 *getExcelModel({ payload }, { call, put }) {
      const { retValue, retCode, retMsg } = yield call(
        services.getExcelModel,
        payload,
      );
      if (retCode === '200') {
        if (retCode === '200') {
          var a = document.createElement('a');
          a.download = `线索.xlsx`;
          a.href = retValue;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          message.success('下载成功');
        }
      }
    },

https://www.cnblogs.com/jdWu-d/archive/2020/07/31/13410664.html

你可能感兴趣的:(前端常见下载功能)