The front end downloads the link and byte stream in the form of xlxs

Download according to the link:

  const todownload = () => {
    var downloadElement = document.createElement('a');
    downloadElement.href =URL;//此处是URL链接
    downloadElement.download = '导入模板.xlsx'; //下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement);
  };

Download by byte stream:

export async function exportAlluserManagement(data: Any) {
  return request('/api/v0/xxx', {
    method: 'post',
    data: { ids: []},
    header: {
      headers: { 'Content-Type': 'application/x-download' },
    },
    responseType: 'blob',
  });
}
    exportAlluserManagement().then(res => {
      console.log(res);
      var headerData = res.headers
      console.log('res', res)
      var fileName = '.xlsx'
      console.log('fileName', fileName)
      fileName = '导出所有数据'
      let blob = new Blob([res], { type: 'application/vnd.ms-excel' })
      var url = window.URL.createObjectURL(blob);
      var aLink = document.createElement("a");
      aLink.style.display = "none";
      aLink.href = url;
      aLink.setAttribute("download", fileName);
      document.body.appendChild(aLink);
      aLink.click();
      document.body.removeChild(aLink); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    })

Guess you like

Origin blog.csdn.net/qq_41160739/article/details/127513158