我正在使用formidable接收node.js上传的文件。我在一个多部分请求中发送了一些字段和一个文件。
一旦某些字段到达,我就能够验证请求的真实性,例如,如果这是不正确的,我想中止整个请求,以避免浪费资源。
我还没有找到中止传入请求的正确方法。我尝试按如下方式使用req.connection.destroy();:
form
.on('field', function(field, value) {
fields[field] = value;
if (!fields['token'] || !fields['id'] || !fields['timestamp']) {
return;
}
if (!validateToken(fields['token'], fields['id'], fields['timestamp'])) {
res.writeHead(401, {'Content-Type' : 'text/plain' });
res.end('Unauthorized');
req.connection.destroy();
}
})但是,这会触发以下错误:
events.js:45
throw arguments[1]; // Unhandled 'error' event
^
Error: Cannot resume() closed Socket.
at Socket.resume (net.js:764:11)
at IncomingMessage.resume (http.js:254:15)
at IncomingForm.resume (node_modules/formidable/lib/incoming_form.js:52:11)
at node_modules/formidable/lib/incoming_form.js:181:12
at node_modules/formidable/lib/file.js:51:5
at fs.js:1048:7
at wrapper (fs.js:295:17)我也尝试过req.connection.end(),但文件一直在上传。
有什么想法吗?提前感谢!
发布于 2011-10-30 16:50:40
问题是formidable不理解你想让它停下来。试试这个:
req.connection.destroy();
req.connection.resume = function(){};当然,这是一个有点丑陋的变通办法,我会open an issue on github。
https://stackoverflow.com/questions/6079697
复制相似问题