Search code examples
node.jsfile-uploadimagemagickmulter

How To Upload Same image in multiple folders Nodejs multer


How to Upload Same Image In Different Folder At Same Time? I was Tried below but not worked But when Upload in one folder it will work.

var multer  = require('multer');

const storage = multer.diskStorage({

    destination: (req, file, cb) => {

        cb(null, 'public/images/image')

        cb(null, 'public/images/pic')
    },

    filename: (req, file, cb) => {

        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)

        cb(null, file.fieldname + '-' + uniqueSuffix+'.png')

    }

});

const cpUpload = multer({ storage: storage });

On frontend:

<input type = "file" name="image" />

<button>Upload</button>

Solution

  • Multer expects a single filename from the destination()-function. So that approach wouldn't work. You could pass single directory in destination(), and then in your controller, after multer has processed the file upload request, copy the uploaded file from one location to other locations using fs.copyFile().