Multer: How I can limit file size?

Created on 16 Feb 2016  路  10Comments  路  Source: expressjs/multer

i use [.fields(fields)] for upload image.
i need limit file size of each fields.

example
upload.fields([{
name: 'avatar', maxCount: 1,
name:'avatar2', maxCount: 2
}]
i need limit file size of avatar (1MB)
i need limit file size of avatar2 (5MB)

Can I do it? How?
thank you

Most helpful comment

You test on this option:

var upload = multer({
    ...
    limits: { fileSize: settings.maxSizeUpload }, //In settings.maxSizeUpload: 1 * 1024 * 1024 // 1MB
  ...
}).fields([{ name: 'avatar', maxCount: 1 }, { name: 'avatar2', maxCount: 2 }]);

In Router.post:

upload(req, res, function(err) {
    if (err) {
        //Handle error
        return res.end("Something went wrong!" + err);
    }
    res.redirect('/users/dashboard');
});

goodluck!

All 10 comments

It isn't supported currently...

thank you LinusU

you can do it as follow:
multer({
storage: storage,
limits:{
fileSize:1000 //size of u file
},
fileFilter:fileFilter
});

i need limit file size of each fields.

I don't think that this use case is solved yet...

There still needs to be a way to provide individual limits for when handling multiple form fields with different fieldnames.

My suggestion:
limits could also take a function as well as an object (default). This function should be called internally with (fieldname, cb) args. The cb function needs to be called with a valid limits object. Doing so enables setting custom limits by reading fieldname.

You test on this option:

var upload = multer({
    ...
    limits: { fileSize: settings.maxSizeUpload }, //In settings.maxSizeUpload: 1 * 1024 * 1024 // 1MB
  ...
}).fields([{ name: 'avatar', maxCount: 1 }, { name: 'avatar2', maxCount: 2 }]);

In Router.post:

upload(req, res, function(err) {
    if (err) {
        //Handle error
        return res.end("Something went wrong!" + err);
    }
    res.redirect('/users/dashboard');
});

goodluck!

@2pay OP's question is about having a different size limit for each field. Your suggestion doesn't solve that.

@TusharRoy23 Thank you for taking the time to post (while keeping it illegible).
However, it doesn't solve the OP's question.
Your piece of code shows how to set up a filter by file-type (MIME-type).

The OP's question is regarding limiting the file size of the valid file(s) that have already passed the fileFilter function.

Reiterating @fndaru 's reply to a previous attempt at an answer:

OP's question is about having a different size limit for each field. Your suggestion doesn't solve that.

2021 and still no solution for this? There are two answers and both are wrong.

@mimoid-prog there is size of file in req.rawHeaders(it a array) in my request last item is size of file so I used (req.rawHeaders.slice(-1)[0]) ,after that I write a logic in fileFilter based on type file, It's work to me, multer should be .any
```
const multer = require('multer');
const { v4: uuid } = require("uuid");

const TYPE_IMAGE = {
'image/png': 'png',
'image/jpeg': 'jpeg',
'image/jpg': 'jpg'
};
const TYPE_File = {
'application/pdf': 'pdf',
};

const fileUpload =
multer({
limits: 500000,
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/images');
},
filename: (req, file, cb) => {
const ext = (TYPE_IMAGE[file.mimetype]) ? TYPE_IMAGE[file.mimetype] : TYPE_File[file.mimetype];
cb(null, uuid() + '.' + ext);
}
}),
fileFilter: (req, file, cb) => {
let size = +req.rawHeaders.slice(-1)[0]
let isValid =false;
if(!!TYPE_IMAGE[file.mimetype] && size < 4 * 1024 * 1024 ){
isValid = true
}
if(!!TYPE_File[file.mimetype] && size < 1 * 1024 * 1024 ){
isValid = true
}
let error = isValid ? null : new Error('Invalid mime type!');
cb(error, isValid);
}
}).any();

module.exports = fileUpload;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BlueOctober picture BlueOctober  路  3Comments

trexanhvn picture trexanhvn  路  3Comments

Gabxi picture Gabxi  路  3Comments

sant123 picture sant123  路  4Comments

kiwenlau picture kiwenlau  路  4Comments