[英]Why am I getting the error “Error [ERR_STREAM_WRITE_AFTER_END]: write after end” when downloading a file?
[英]I am getting ERR_STREAM_WRITE_AFTER_END in node js api. How to fix this issue?
我從ui中點擊上傳文件api時遇到以下問題。 我正在從react js命中節點js api,然后從node js命中公共api。
節點版本:-10.15.3
npm版本:-6.4.1
router.route("/uploadLocaleFile").post(function(req, res, next) {
req.on("data", data => {
uploadFile(req, data, res);
});
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
試試這個代碼
router.route("/uploadLocaleFile").post(function(req, res, next) {
const chunks = [];
req
.on('data', data => chunks.push(data))
.on("end", _ => uploadFile(req, Buffer.concat(chunks), res)
;
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
您可能需要嘗試@Yaroslav Gaponov解決方案。
您的問題是.pipe(res)
在每個data
事件上都觸發。 到第二個data
事件發生時, res
流已經關閉。
@Yaroslav Gaponov所做的是,在每個data
事件中,他將傳入的緩沖區存儲到數組中。 當流完成時,將發出end
事件,然后才允許您對res
進行1次寫入,即uploadFile中的.pipe(res)
。
您也可以嘗試request
一個管線管道,如下所示:
req.pipe(請求(your_options))。管(RES)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.