我正在使用强大的上传文件。当上传失败时(例如,当 uploadDir 不可写时),form.on('error') 不会处理错误,而是未捕获的异常。如何处理上传错误?这基本上是 fromidable 的自述文件中的示例代码,带有不存在的 uploadDir 和错误处理程序。
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.uploadDir = '/foo/'; // this does not exist
form.on('error', function(error) { // I thought this would handle the upload error
console.log("ERROR " + error);
return;
})
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8000);
我收到的错误是:
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: ENOENT, open '/foo/9b4121c196dcf3f55be4c8465f949d5b'