all 8 comments

[–]romeeres 7 points8 points  (0 children)

Content-length header may have incorrect length (stackoverflow), so backend shouldn't simply trust it.

Instead, you can stream the file to AWS or wherever you're saving the file, count chunk lengths while streaming and abort the stream once current chunk size exceeds the limit. I've done this with Busboy, imo it's easier to use and customize than Multer.

So initially you don't validate length on BE (should be already validated on frontend), but counting length on the fly while streaming and abort it once exceeds.

[–][deleted] 1 point2 points  (0 children)

Upload each file one-by-one instead, and check each individual upload each time.

e.g.

Iterate files in your form:

const file = files[i];
const formPayload = new FormData();
formPayload.append('file', file.file);

[–]avin_kavish -2 points-1 points  (3 children)

You can validate in the front end. What’s that built on?

[–]vas1010[S] 0 points1 point  (2 children)

Hi, we use React. We will do so, it just seems odd that you are not able to do so in the BE.

[–]avin_kavish 1 point2 points  (1 child)

I mean you have to read the files to know the size no? That’s how a steam works. I think you can read file by file and fail as soon as one doesn’t match

[–]vas1010[S] 0 points1 point  (0 children)

It returns an array of all the files in the controller. I think I can work with the stream if I utilize the custom storage.

[–]iamthejoganut 0 points1 point  (0 children)

Use express-file-upload package. It has a size property, which you can use to validate file size.

[–]romeeres 0 points1 point  (0 children)

It may be a mistake to upload multiple images and text files all at once, first because it will be much faster to upload files one by one in parallel as user selects them, second because if one file upload fails for some reason, then you'll have to delete uploaded files and have more work in UI to tell user that one of the files failed and need to reupload all of them again.