3

This is my multer code to upload multiple files.

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/files/'+ req.user.id)
  },
  filename: function (req, file, cb) {
    x = file.originalname; //+path.extname(file.originalname);
  cb(null,x);
  }
});

var upload = multer({storage: storage});

This is the post request where files get submitted on click submit.

router.post(upload.array("FileUpload",12), function(req, res, next) {

//Here accessing the body datas.

})

So what I want is that, I want to create a folder first with the name of the ID generated which can be access from the req.body and then upload those files into that folder respectively. But since I cannot access the body first before upload I am unable to create that respective folder directory. Is there any other way around which I can think of and implement this?

Updated Solution using fs-extra package.

const multer = require('multer');
let fs = require('fs-extra');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    let Id = req.body.id;
    fs.mkdirsSync('./public/files/'+ req.user.id + '/' + Id);
    cb(null, './public/files/'+ req.user.id + '/' + Id)
  },
  filename: function (req, file, cb) {
    x = file.originalname; //+path.extname(file.originalname);
  cb(null,x);
  }
});

var upload = multer({storage: storage});

This is the post request where files get submitted on click submit.

router.post(upload.array("FileUpload",12), function(req, res, next) {

//Here accessing the body datas.

})

1 Answer 1

3

you have to install first the fs-extra which will create folder

create seprate folder for multer like multerHelper.js

const multer = require('multer');
let fs = require('fs-extra');

let storage = multer.diskStorage({
destination: function (req, file, cb) {
    let Id = req.body.id;
    let path = `tmp/daily_gasoline_report/${Id}`;
    fs.mkdirsSync(path);
    cb(null, path);
},
filename: function (req, file, cb) {
    // console.log(file);

    let extArray = file.mimetype.split("/");
    let extension = extArray[extArray.length - 1];
    cb(null, file.fieldname + '-' + Date.now() + "." + extension);
 }
})

let upload = multer({ storage: storage });

let createUserImage = upload.array('images', 100);

let multerHelper = {
    createUserImage,
}

module.exports = multerHelper;

in your routes import multerhelper file

const multerHelper = require("../helpers/multer_helper");

router.post(multerHelper , function(req, res, next) {

//Here accessing the body datas.

})
Sign up to request clarification or add additional context in comments.

5 Comments

Where to find helpers/multer_helper?
you have to create it in your project folder
I have updated the solution according to your answer. Is it correct? Please check after "Updated Solution using fs-extra package" added in my question.
I have created sepreate folder for routes also so this method worked for me
Didn't test yet. Will do it and let you know if it works. And then I will upvote your answer as well for sure thanks! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.