question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I get file size inside fileFilter function?

See original GitHub issue

Am I have any way to get file size inside fileFilter function?

var fileFilter = function (req, file, cb) {
  console.log(file.size); // undefined
}

I have only four first keys in file variable

Key Description Note
fieldname Field name specified in the form
originalname Name of the file on the user’s computer
encoding Encoding type of the file
mimetype Mime type of the file
size Size of the file in bytes
destination The folder to which the file has been saved DiskStorage
filename The name of the file within the destination DiskStorage
path The full path to the uploaded file DiskStorage
buffer A Buffer of the entire file MemoryStorage

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

25reactions
LinusUcommented, Jul 31, 2015

Oh, actually that is very easily solvable in a clean way for you!

var limits = { fileSize: 1024 * 1024 * 1024 }
var upload = multer({ limits: limits })

app.post('/upload', upload.single('file'), function (req, res) {
  res.send({ result: 'ok' })
})

app.use(function (err, req, res, next) {
  if (err.code === 'LIMIT_FILE_SIZE') {
    res.send({ result: 'fail', error: { code: 1001, message: 'File is too big' } })
    return 
  }

  // Handle any other errors
})

The fileSize limit will automatically remove the file if it received a too large file, read more in the readme 😃

2reactions
LinusUcommented, Jul 31, 2015

I’m sorry to say that file.size is only available after the file has been received.

This could possibly be fixed by looking at the Content-Length header of that part, but I do not yet know if browsers send those.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to limit the file size when uploading with multer?
var maxSize = 1 * 1000 * 1000; var storage = multer. diskStorage({ destination: function (req, file, callback) { callback(null, 'public/upload' ...
Read more >
Node.js Multer File Upload Type Validation Filters and Limit ...
Node.js Multer File Upload Type Validation Filters and Limit File Size and Error Handling in ExpressDownload the full source code of ...
Read more >
Express multer middleware
File information ; mimetype, Mime type of the file ; size, Size of the file in bytes ; destination, The folder to which...
Read more >
<input type="file"> - HTML: HyperText Markup Language | MDN
The returnFileSize() function takes a number (of bytes, taken from the current file's size property), and turns it into a nicely formatted size ......
Read more >
Configuring Express Multer Middleware and Checking File ...
Defaults to Infinity; fileSize — maximum file size in bytes. ... The fileFilter field is a function that lets us control which files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found