如何在React上下载zip?

时间:2018-11-21 23:43:20

标签: node.js reactjs axios

在nodejs上,我正在按以下方式发送文件:

res.sendFile(fileName,{ root: path.join(__dirname, '../') });

在做出反应时,我一直试图用axios将文件下载成功,但没有成功:

let dowloadGet = async (url, auth) => 
await axiosDownload.get(url, { headers: { 'Authorization': `Bearer ${auth}` }, responseType: 'blob' });

  let response = await API.get(`${this.state.BE_route}/download_script_bytest/${this.props.test._id}`, this.props.idToken)

  const url = window.URL.createObjectURL(new Blob([response.data], {type:'application/zip'}));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'file.zip');
  document.body.appendChild(link);
  link.click();
  link.parentNode.removeChild(link);

0 个答案:

没有答案