【发布时间】:2019-01-30 07:02:27
【问题描述】:
我正在向我的服务器发送数据,该服务器根据请求创建一个 pdf 文件,该请求创建得很好,但我无法将文件发送回客户端。我正在使用 React 提交表单
handleSubmit(event) {
event.preventDefault();
var body = {
id: this.state.id,
text: this.state.text
}
fetch('http://localhost:5000/pdf', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
},
}).then(function(file) {
window.open(file.url);
});
}
它的开放http://localhost:5000/pdf,但由于我没有它的 GET 路径,所以没有任何下载。这是我的 POST 路线
router.post('/pdf', async function(req, res) {
var makePdf = require('./file-create/pdf.js');
makePdf(req, res);
});
文件被返回为pdfDoc.pipe(res);
我不能只使用 GET 路由,因为我无法获取通过这种方式发送的数据,我怎样才能让这个文件发送到客户端?
【问题讨论】:
-
是的,它会下载文件但没有内容/加载失败
标签: javascript node.js reactjs