expressjs / expressjs/multer

File data gets saved with wrong file name for multi-file uploads

Open
#307 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Hello. If I upload two files, one .json and the other a text file, and the text file is empty, the file data gets swapped, and the json data gets stored to the text file's name. I have also verified that if I upload multiple json files that contain valid json, the files get saved correctly to the right files. And then, in running another test, where I upload two valid json files and one empty text file, the error condition occurs again (one of the json files winds up with 0 bytes, and one of the json files gets saved to the text file's name).

This is a huge problem, so I hope you can help.

Here is how I am configuring multer, and I am testing the upload using postman:


function getStorage(config) {
return multer.diskStorage({
destination: function(req, file, cb) {
// Controls where the file is stored
cb(null, config.dest);
},
filename: function(req, file, cb) {
// Controls what the file name will be. Existing file names will be overwritten.
cb(null, file.originalname);
}
});
}

function createUploadModule(config) {
return multer({ dest: config.dest, storage: getStorage(config)});
}

function configureUploadModule(config) {
module.exports.uploadConfig = config;
module.exports.uploadModule = createUploadModule(config).array(config.fileNameFormKey);
}

Please note that I have also tried removing the file rename function from getStorage(), but the same behavior still occurs, even when multer generates the file names.

Thanks for any insight on this.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.