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.

Handling "File too large" properly

See original GitHub issue

So, I’m using this code:

const multer = require(`multer`);
let storage = multer.diskStorage({
	destination: function (req, file, cb) {
		cb(null, 'usercontent/avatar/')
	},
})
const upload = multer({
	storage,
	limits: {
		fileSize: 2000000,
		files: 1,
	}
});

Now, that works. However, when a user inputs a file bigger than 2000000 bytes (2MB) the only thing that happens is an error stack in the console.

How could I display the error to the end-user by either: sending back JSON content or redirecting and silence the error in the console? Checking if !req.file in the POST request won’t suffice, as the field is optional and can be blank (which is expected and checked for). I tried to try/catch both the upload variable and the post request but the error sits somewhere deeper.

If I could use a function to verify the file size before the saving occurs (just like in destination or filename), then this wouldn’t be a problem but is currently, as the object only takes a plain number (correct me if wrong).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
NorteX-devcommented, Sep 28, 2020

Ok, since nobody is responding and this thread is basically I’m closing it and I will attempt to fix it myself in the source code.

0reactions
simondixitcommented, Mar 13, 2022

Hey there! I set this in my middlewares and it does work! =)

    this.server.use(
      '/api/v1/upload',
      (req: express.Request, res: express.Response, next: express.NextFunction) => {
        upload(req, res, (err: any) => {
          if (err) {
            res.status(500).json(err)
          }
          next()
        })
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix 'File Is Too Large for Destination File System ... - EaseUS
Get 'The file is too large for the destination file system' error message while copying larger files with size more than 4GB to...
Read more >
4 Ways to Fix File Is Too Large for the Destination File System ...
Method 1: Compressor Split the big files ... When the file size is too large then compress or split it to save it...
Read more >
File too large for USB drive? Here is how to fix it.
Press Start to begin the formatting, and when it's done, press Close to dismiss the Format screen. Now you should be able to...
Read more >
How to Fix the "File Is Too Large for the Destination File ...
When moving files between your hard drives, you may encounter the "File is too large for destination file system" error.
Read more >
They all shall pass: a guide to handling large file uploads
Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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