我有nodejs服务器:
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
console.log(req.rawHeaders)
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
if (err) throw err;
var oldpath = files.file.path
var newpath = 'C:/VSI/' + files.file.name;
fs.rename(oldpath, newpath, function (err) {
//if (err) throw err;
//res.write('File uploaded and moved!');
if (err) {
res.write(JSON.stringify({
success: false,
message: 'error happend'
})
);
res.end()
throw err;
}
res.write(JSON.stringify({
success: true,
message: 'Successfully Uploaded'
})
);
res.end();
});
});
} else {
console.log(req.rawHeaders)
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080, () => {
console.log('server running')
}).on("error", () => console.log("error happend"));
得到
if (err) throw err;
^
Error: maxFileSize exceeded, received 209715683 bytes of file data
我如何上传更多200 MB的强大?
如何正确处理异常以及为什么“错误”(“错误”,()=>)事件不起作用“?
我尝试了Multer,它没有200 MB的限制并正常工作。
只需阅读文档:https://www.npmjs.com/package/formidable
默认最大大小为200MB,更改为(例如300MB):
form.maxFileSize = 300 * 1024 * 1024;