Multiple file fails / corrupted image uploaded
See original GitHub issueMulter work great for single image, but fails for multiple file uploads.
Problems I am facing with multiple uploads:
- Multer is configured to upload maximum of 6 photos, I get only 4 or 5 photos in my folder.
- I got corrupted images in my folder
Corrupted Image 1
Corrupted Image 2
Original Image
Multer Config
const MulterStorage = multer.diskStorage({
destination: async function (req, file, callback) {
const uploadPath = `public/images/${req.userId}`;
callback(null, uploadPath);
},
filename: async function (req, file, callback) {
const fileExt = file.originalname.substring(file.originalname.lastIndexOf('.')).toLowerCase();
callback(null, `${await RandomUtils.uuid()}${fileExt}`);
}
});
const uploadProfilePhoto = multer({
storage: MulterStorage,
fileFilter: function (req, file, callback) {
const fileName = file.originalname.toLowerCase();
if (!fileName.match(/\.(jpg|jpeg|png)$/)) {
callback(new Error('Only JPG, JPEG & PNG Image Formats are only allowed!'));
}
callback(null, true);
},
limits: { fileSize: 800 * 800 },
preservePath: true
}).array('photo', 6);
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Corrupted Image Repair Guide to Recover Damaged Photos ...
Are your photos corrupted? Here's is a guide on why your photos get damaged, how to perform corrupted image repair, and how you...
Read more >How to Fix Corrupted Files - Lifewire
Corrupt file errors can be unpredictable and can happen when least expected. A corrupt file can be repaired only about half the time....
Read more >Getting corrupted PDF files on multiple uploads - Jotform
Getting corrupted PDF files on multiple uploads. Profile Image ... I want to continue with Jotform but cannot with these types of failures....
Read more >How to repair corrupted JPEG file - YouTube
how to repair corrupted jpeg filehow to repair damaged jpg filehow to fix shifted colour color changed imagehow to recover image with grey ......
Read more >How to Repair Corrupt Photos in Windows 10
Further, there could be a case where the recovered photos may be corrupt. That happens due to file overwriting as you continue using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I was able to reproduce this with the code you supplied and the original image you supplied.
Unfortunately I think your original image is corrupted. Even when uploading it as a single image, the saved image was corrupted:
When running the original image through ImageMagick’s
identify
tool to check if the image has any issues, per this Stack Overflow answer I got this output which seems to suggest the original image is corrupted:I know this issue is resolved, but I wanted to share my experience with this problem.
I’m not and expert, but from my testing it looks like multer isn’t handling async functions for
filename
anddestination
. I have had the same problem with corrupted files, but I was using async functions. When I turned them into regular function there was not problem and everything was working fine.