node.js 如何在 express.js 中限制上传文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13374238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to limit upload file size in express.js
提问by paynestrike
i got this error says
我收到这个错误说
error:request entity too large
when uploading a video about 30MB,
上传大约 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!!!
不知道如何设置 maxFieldsSize 属性,需要一些帮助!!!
回答by Sdedelbrock
Express uses connect middleware, you can specify the file upload size by using the following
Express 使用 connect 中间件,可以通过以下方式指定文件上传大小
app.use(express.limit('4M'));
回答by Trí C?ng V?
// 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
回答by Will Hancock
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.
取决于您使用的版本。
回答by shodgkins
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.
我正在使用 Express 4。我尝试了许多 app.use() 语句,包括此线程中列出的未弃用的语句,但没有一个起作用。
Instead, it turned out I only needed to add one line to index.js to change the maxFileSize option in the Formidable module:
相反,结果我只需要向 index.js 添加一行即可更改 Formidable 模块中的 maxFileSize 选项:
// 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.
来源:来自这里的评论。
回答by Meisterunner
In 2020 Express uses the body-parser urlencoded function to control the limit. http://expressjs.com/en/4x/api.html#express.urlencoded
2020 Express 使用 body-parser urlencoded 函数来控制限制。 http://expressjs.com/en/4x/api.html#express.urlencoded
These are the default settings inside node_modules>body-parser>lib>types>urlencoded.js https://www.npmjs.com/package/body-parser
这些是 node_modules>body-parser>lib>types>urlencoded.js https://www.npmjs.com/package/body-parser中的默认设置
var extended = opts.extended !== false
var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
var type = opts.type || 'application/x-www-form-urlencoded'
var verify = opts.verify || false
You can see here that the default setting for limit is 100kb. so in order to up that you can use
您可以在此处看到限制的默认设置为 100kb。所以为了提高你可以使用
app.use(express.urlencoded({ extended: false, limit: '2gb' }));
here are the filetype options available via NPM package bytes ( used by bodyparser ) https://www.npmjs.com/package/bytes
这是通过 NPM 包字节可用的文件类型选项(由 bodyparser 使用) https://www.npmjs.com/package/bytes
"b" for bytes
"kb" for kilobytes
"mb" for megabytes
"gb" for gigabytes
"tb" for terabytes
"pb" for petabytes
I'm sure this was overkill but I hope this helps the next person.
我确定这有点矫枉过正,但我希望这对下一个人有所帮助。
回答by Aashish Prajapat
var upload = multer({ storage : storage2, limits: { fileSize: 1024 * 1024 * 50 } });
correct format for increasing file uploading size with multer in nodejs
在nodejs中使用multer增加文件上传大小的正确格式

