我不知道为什么会这样,但这真的很烦人。我预计该文件将根据快速文档 https://expressjs.com/en/api.html#res.download。我有下一个代码:
//in react (App.js)
download = () => {
const { images, target } = this.state;
const filename = images.find(img => img.id === target).filename;
fetch('http://localhost:3001/download', {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({filename})
})
}
//in express (server.js)
app.post('/download', (req, res) => {
var file = '../public/images/' + req.body.filename;
res.download(file, req.body.filename);
});
文件夹结构:
-server
|-server.js
-public
|-images
|-a.jpg
|-b.jpg
-src
|-App.js
什么也没发生,没有显示错误。有任何想法吗?
我认为您遇到的问题是您使用的是 HTTP POST 而不是 HTTP GET
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)