How to limit upload file size in express.js
我得到这个错误说
1 | error:request entity too large |
上传大约30MB的视频时,
这是设置代码
1 2 3 4 | app.use(express.bodyParser({ uploadDir:'./Temp', maxFieldsSize:'2 * 1024 * 1024 * 1024 ', })); |
不确定如何设置maxFieldsSize属性,需要一些帮助!!!
Express使用连接中间件,您可以使用以下命令指定文件上传大小
1 | app.use(express.limit('4M')); |
连接限制中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Comment sart // app.use(express.bodyParser({ // uploadDir:'./Temp', // maxFieldsSize:'2 * 1024 * 1024 * 1024 ', // })); // Add this code for maximun 150mb app.use(bodyParser.json({limit: '150mb'})); app.use(bodyParser.urlencoded({ // to support URL-encoded bodies limit: '150mb', extended: true })); // I did it Okay. Goood luck |
1 | app.use(express.limit('4mb')); |
但是您必须确保在以下内容的上方添加此行,
1 | app.use(express.bodyParser()); |
要么
1 2 | app.use(express.json()); app.use(express.urlencoded()); |
取决于您使用的版本。
我正在使用Express4。我尝试了许多app.use()语句,包括此线程上列出的不推荐使用的语句,但没有一个起作用。
相反,事实证明,我只需要向index.js添加一行即可更改Formidable模块中的maxFileSize选项:
1 2 3 4 5 | // create an incoming form object var form = new formidable.IncomingForm(); // ADD THIS LINE to increase file size limit to 10 GB; default is 200 MB form.maxFileSize = 10 * 1024 * 1024 * 1024; |
资料来源:这里的评论。
1 | var upload = multer({ storage : storage2, limits: { fileSize: 1024 * 1024 * 50 } }); |
在Node.js中使用multer增加文件上传大小的正确格式