How can I get updated file names? #1192
Answered
by
zanzlender
zanzlender
asked this question in
Q&A
-
I'm updating the file names to be a UUID so I don't get conflicts like so: const storage = multer.diskStorage({
// define location where to store
destination: function (request, file, cb) {
cb(null, './public/shared');
},
// change file name before saving it
filename: function (request, file, cb) {
const uniqueName = randomUUID() + '.' + file.mimetype.split('/')[1];
cb(null, uniqueName);
},
}); And then in my express routes I define: const UploadRequest = MulterUploader.array('files');
api.post('/', async (req, res) => {
UploadRequest(req, res, function (err) {
if (err instanceof multer.MulterError) {
console.log('Error while uploading \n -------------------------');
return res.status(400).send({ code: err.code, message: err.message, name: err.name });
} else if (err) {
console.log('Error while uploading \n -------------------------');
return res.status(400).json({ code: err.code, message: err.message, name: err.name });
}
console.log('Successfully uploaded file.');
return res.status(200).json({
files: req.files,
message: 'Upload successful',
code: 'SUCCESS',
});
});
}); Req.files gives the original files w/ filenames Multer received. However, how can I get the newly generated file names created in the DiskStorage filename function? |
Beta Was this translation helpful? Give feedback.
Answered by
zanzlender
Mar 14, 2023
Replies: 1 comment
-
Found the answer in #302 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zanzlender
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found the answer in #302 (comment)