gpt4 book ai didi

file-upload - 在 NodeJs 中上传文件时停止请求

转载 作者:搜寻专家 更新时间:2023-10-31 23:39:31 25 4
gpt4 key购买 nike

我正在编写一个图片 uploader ,我想将图片的大小限制在 3mb 以下。在服务器端,我可以检查 header 中图像的大小,如下所示(使用 express):

app.post('/upload', function(req, res) {
if (+req.headers['content-length'] > 3001000) { // About 3mb
// Do something to stop the result
return res.send({'error': 'some kind of error'});
}
// Stream in data here...
}

我试图通过(和排列)停止请求

req.shouldKeepAlive = false;
req.client.destroy();
res.writeHead(200, {'Connection': 'close'});
res.end()

它们都没有真正“破坏”阻止上传更多数据的请求。req.client.destroy() 似乎卡住了下载,但是 res.send({error... 没有被发回。

帮助!

最佳答案

抛出一个错误并捕获它。它将停止文件上传,允许您发送响应。

try { throw new Error("Stopping file upload..."); } 
catch (e) { res.end(e.toString()); }

这有点hackish,但它确实有效......

关于file-upload - 在 NodeJs 中上传文件时停止请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com