multer icon indicating copy to clipboard operation
multer copied to clipboard

How I can limit file size?

Open realsagi opened this issue 9 years ago • 12 comments

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

realsagi avatar Feb 16 '16 10:02 realsagi

It isn't supported currently...

LinusU avatar Feb 16 '16 14:02 LinusU

thank you LinusU

realsagi avatar Feb 17 '16 03:02 realsagi

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

djlxiaoshi avatar May 20 '16 01:05 djlxiaoshi

i need limit file size of each fields.

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

LinusU avatar Jan 20 '17 09:01 LinusU

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.

jagged3dge avatar Sep 09 '17 10:09 jagged3dge

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 avatar Dec 04 '17 03:12 2pay

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

fndaru avatar Dec 04 '17 18:12 fndaru

@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.

jagged3dge avatar Jan 08 '19 07:01 jagged3dge

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

mimoid-prog avatar Jan 29 '21 11:01 mimoid-prog

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

myas92 avatar Jan 30 '21 08:01 myas92

Any updates on this issue?

Saikedo avatar Nov 10 '21 15:11 Saikedo

no more update at 2023?

RaphaelCaputo2 avatar Mar 28 '23 19:03 RaphaelCaputo2