DEV Community 👩‍💻👨‍💻

Cover image for Uploading multiple files with multer, but from different fields Node JS
zeeshan mehdi
zeeshan mehdi

Posted on • Updated on

Uploading multiple files with multer, but from different fields Node JS

i was in a situation where i were to upload audio file along with a graphic for that particular song so i was to upload to different files an image and audio file so i used a way to do that using multer plugin available on link

var multer = require('multer')
var storage = multer.diskStorage({

destination: function(req, file, callback) {
    callback(null, './public/audio');
  filename: function(req, file, callback) {
      callback(null, file.fieldname + '-' + + file.originalname.substr(file.originalname.length-6,file.originalname.length));
      callback(null, file.fieldname + '-' + + file.originalname);


const upload = multer({ storage: storage });'/save/audio',upload.fields([{
  name: 'audio', maxCount: 1
}, {
  name: 'graphic', maxCount: 1
}]) ,(req, res) => {

  const audioFile =[0];
  const audioGraphic = req.files.graphic[0];
  const fileName = req.body.title;

  saveAudio(fileName,audioFile.filename,audioGraphic.filename,req.body.artist,function (error,success) {
    req.flash('success','File Uploaded Successfully')


Enter fullscreen mode Exit fullscreen mode

Top comments (3)

rshrmcse profile image

this one is really useful and less code

farzanfx profile image

Thanks for snippet it saved me a lot of time!

zeeshanmehdi profile image
zeeshan mehdi Author

glad to hear

🌚 Life is too short to browse without dark mode