我正在尝试使用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);
}
};
任何帮助将不胜感激。