Express res.download()不会在浏览器中触发下载

时间:2019-07-04 09:32:51

标签: javascript node.js reactjs

我正在尝试使用res.download下载文件,但是它不会触发浏览器下载,但是会将文件数据发送到控制台,如何触发浏览器下载?

router.get('/downloadFile/:clientId/:item', auth, async (req, res) => {
  const { clientId, item } = req.params;
  const dir = `./public/uploads/${clientId}`;
  const file = `${dir}/${item}`;

  res.download(file, item, err => {
    if (err) {
      console.log(err);
      return res.status(500).send('There was a problem with download');
    } else {
      return res.send('Download succesful!');
    }
  });
});

触发下载的动作

export const downloadFile = (clientId, item) => async dispatch => {
  try {
    const res = await axios.get(`api/pdf/downloadFile/${clientId}/${item}`);
    console.log(res.data);
    dispatch({
      type: DOWNLOAD_FILE
      //   payload: res.data
    });
  } catch (error) {
    console.log(error);
  }
};

任何帮助将不胜感激。

0 个答案:

没有答案