New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multer does not throw an error when limits: fileSize is exceeded and hangs #602
Comments
I have exactly the same issue. I completely removed this option and handle files which are too big on my own |
yup, I handle the file size on the client as well. Though I think it is my custom storage engine that messes things up. Based on other issues i read the default disk storage engine may be able to handle file limits at least for one file. I have not had a chance to look at this in any detail. Also may be related to #447 . Seems the error handling is a bit tricky. |
"Though I think it is my custom storage engine that messes things up" "Seems the error handling is a bit tricky." I think the issue might have something to do with the file streams. As far as I know, the file streams for writing to the file system have more meta information than, e.g. streams to google storage buckets. |
I am also facing the same issue. It doesn't throw an error when the file limit exceeded. |
I have not defined fileSize limits but Multer does not throw an error but keeps hanging when uploading larger files( like > 1MB). |
I'm not sure if this is a bug but I'm labeling and self assigning to look at it another time |
I was unable to reproduce this issue. Here is a codesandbox where I've attempted reproduction. When Leaving this open for now to wait for more info, if anyone is experiencing this please provide a reproduction case if you're able to! ❤️ |
I am currently using the latest version 1.4.2 and I have the following limit set: It still doesn't throw any error |
@nonsobiose Can you provide a minimal reproduction case? I haven't been able to reproduce this so far and will need to see your code to try and debug. |
|
@nonsobiose I would need to see a standalone example of your code where you're experiencing the issue to be able to figure out what's happening, but see my earlier comment with the link to a codesandbox where I'm handling the error passed to the upload callback triggered by the When uploading the file with your |
This is so weird. It works perfectly on your sandbox @jonchurch . I'm here because I tried to get the file filter errors working:
Which returns
But the sandbox that I made seems to be working perfectly Edit: I ended up manually setting Inspired by this answer: https://stackoverflow.com/a/35069987/1800515 |
The problem still persists, did anyone got solution of this problem ? |
According to documentation here: https://github.com/expressjs/multer#error-handling I tried to follow the error handling this way: if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
} But the error is not an instanceOf MulterError but HttpError, and follows the next structure: {
message: {
error: "Payload Too Large",
message: "File too large",
statusCode: 413
},
response: {
error: "Payload Too Large",
message: "File too large",
statusCode: 413
},
stack: "...",
status: 413,
__proto__: ...
} I used this (simplified) workaround using NestJS: import {HttpException } from '@nestjs/common';
if (err instanceof HttpException) {
if (err.getStatus() === 413) {
// response.status(statusCode).json(response);
} |
remove this: to this: |
I faced this error using multer in nest.js app. It seems like multer just ignores Multer's version is 1.4.2. |
Same issue here using multerGoogleStorage. |
@jonchurch Same issue here using multerGoogleStorage |
this is a solution worked for me , first i configured multer : then in my routes i use it like this :
|
This is causing issues on production for me; while it does in fact throw an error when a file is too big, it does not appear to check the file size by comparing each chunk in the stream, but rather it waits until it has received the entire file and then throws an error. I tried curling I am using:
but it's still happy to let me throw gigabytes of garbage at it only to calculate the total file size at the very end. |
I may have determined the (partial) cause of this. The busboy event |
Hi, I was having issues with this. When I put the limits before the storage (and outside the storage part) it started working for me. Not exactly the same, just hoping to help someone. const maxSize = 1 * 1000 * 1000; |
I am having same problem right now. Did you fix your problem? |
i got error from this ...limits: {fileSize: 500} |
Still suffering from this in 2022... can someone help? |
Me too facing the issue can some one please help ? |
2022 and the error sitlls, Usings NestJS I use this, but is useless: @patch() |
I can't limit less than 1MB. A solution is use |
same too, how to solve custom limit error? |
in case anyone else tried to use string like me, e.g. '4mb', it does not work. use integers, 102410244 |
i have same issue |
did you try using integer gor filesize as I suggested above? worked for me |
|
I believe it is fileSize you wrote fieldSize |
me too but bigger than your size |
I've added an additional middleware to handle file limits middleware export const processFileMiddleware = multer({
storage: memoryStorage(),
}).single('file');
export const fileLimitsMiddleware = asyncHandler(
(request: express.Request, response: express.Response, next: express.NextFunction) => {
if (!request.file) {
throw new ServerError({
code: ErrorCodes.FILE_NOT_FOUND,
message: 'File not found',
statusCode: 400,
});
}
if (request.file.size > config.fileStorage.maxFileSize) {
throw new ServerError({
code: ErrorCodes.FILE_TOO_LARGE,
message: `File too large. max ${humanFileSize(
config.fileStorage.maxFileSize
)}, received ${humanFileSize(request.file.size)}`,
statusCode: 400,
});
}
next();
}
); later I've used protectedRouter.post('/file/upload', processFileMiddleware, fileLimitsMiddleware, uploadFileController); working tests with vitest and supertest. make sure to have valid file paths import supertest from 'supertest';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import { Express } from 'express';
import { Server } from 'http';
import path from 'path';
import { initServer } from '../app';
describe('Upload file', () => {
let app: Express;
let server: Server;
let request: supertest.SuperTest<supertest.Test>;
beforeAll(async () => {
({ server, app } = await initServer());
request = supertest(app);
});
afterAll(() => {
server.close();
});
describe('POST /api/file/upload', () => {
const bigFile = path.join(__dirname, './mocks/big.pdf');
const smallFile = path.join(__dirname, './mocks/small.pdf');
test('should upload successfuly', async () => {
const res = await request
.post('/api/file/upload')
.set({ auth: 'any-token' })
.attach('file', smallFile);
expect(res.body).toEqual({ message: 'File uploaded successfully' });
expect(res.status).toBe(200);
});
test('should return 400 if file is too large', async () => {
const res = await request
.post('/api/file/upload')
.set({ auth: 'any-token' })
.attach('file', bigFile);
expect(res.status).toBe(400);
expect(res.body).toEqual({
errorCode: 'FILE_TOO_LARGE',
message: 'File too large. max 1 byte, received 132 byte',
statusCode: 400,
});
});
});
}); |
|
I ended up handling file size limit by myself
|
I have the following code to handle profile pic uploads. Everything works fine if under the limits: fileSize, but when fileSize is exceeded no error is thrown. Error handling based on #336. I have a custom disk storage engine. The problem with this is that I cannot send any error message or a response to react frontend.
The post method:
Log output when uploading 75KB:
Log output when uploading 190KB:
The text was updated successfully, but these errors were encountered: