How to limit upload file size in express.js

别来无恙 提交于 2019-11-29 20:32:36

问题


i got this error says

error:request entity too large 

when uploading a video about 30MB,

here is the setting code

app.use(express.bodyParser({
    uploadDir:'./Temp',
    maxFieldsSize:'2 * 1024 * 1024 * 1024 ',
}));

am not sure how to set the maxFieldsSize property, need some help!!!


回答1:


Express uses connect middleware, you can specify the file upload size by using the following

app.use(express.limit('4M'));

Connect Limit middleware




回答2:


// 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 



回答3:


app.use(express.limit('4mb'));

But you must make sure you add this line above the below,

app.use(express.bodyParser());

or

app.use(express.json());
app.use(express.urlencoded());

depending on which version you are using.




回答4:


I'm using Express 4. I tried numerous app.use() statements, including the non-deprecated ones listed on this thread, but none of them worked.

Instead, it turned out I only needed to add one line to index.js to change the maxFileSize option in the Formidable module:

// 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;

Source: Comments from here.




回答5:


var upload = multer({ storage : storage2, limits: { fileSize: 1024 * 1024 * 50 } });

correct format for increasing file uploading size with multer in nodejs



来源:https://stackoverflow.com/questions/13374238/how-to-limit-upload-file-size-in-express-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!