【发布时间】:2019-01-29 09:26:07
【问题描述】:
在 React 中,我有超链接,它们使用 Express 从后端节点服务器启动 PDF 文件的获取。问题是流打开了一个新的二进制文本窗口而不是 PDF 文件。
反应前端:
//Link
<a href={'#'} onClick={() => this.renderPDF(row.row.pdfid)}> {row.row.commonName}.pdf</a>
//Fetch call
renderPDF = (pdfLink) => {
fetch('http://localhost:8000/pdf' + '?q=' + pdfLink, {
method: 'GET'
//credentials: 'include'
})
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob))
.then(url => window.open(url))
.catch(error => this.setState({
error,
isLoading: false
}));
}
节点后端:
app.get('/pdf', (req, res) => {
let readStream = fs.createReadStream(req.query["q"]);
let chunks = [];
// When the stream is done being read, end the response
readStream.on('close', () => {
res.end()
})
// Stream chunks to response
readStream.pipe(res)
});
任何意见将不胜感激。
【问题讨论】:
-
什么是readStream?它是在哪里定义的?
-
我编辑了帖子以突出显示重要的内容。
标签: javascript node.js reactjs blob fetch