1
votes

i want uplaod tow file in one request in nodejs and i using the moulter for this work .

this is my request in postman :

enter image description here

and i using the multer in routing :

router.post(
  "/Create",
  UploadProfileHandler.single("signerProfile"),
  UploadPosterHandler.single("signerPoster"),
  SignerValidation.CreateHandle(),
  SignerController.CreateSigner
);

and this isnto the multer :

signer Profile Multer :

const multer = require("multer");
const fs = require("fs");
const mkdirp = require("mkdirp");
const path = require("path");

const GetDirectory = () => {
  let year = new Date().getFullYear();
  let month = new Date().getMonth();
  let day = new Date().getDay();
  return `src/public/uploads/signer/profile/${year}/${month}/${day}`;
};

const SignerStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    console.log(file,req.body)
    let dir = GetDirectory();
    mkdirp(dir).then((made) => {
      cb(null, dir);
    });
  },
  filename: (req, file, cb) => {
    let fileName = GetDirectory() + "/" + file.originalname;
    cb(null, file.originalname);
  },
});

const UploadSigner = multer({
  storage: SignerStorage,
});

module.exports = UploadSigner;

and this is singer Poster Multer :

    const multer = require("multer");
const fs = require("fs");
const mkdirp = require("mkdirp");
const path = require("path");

const GetDirectory = () => {
  let year = new Date().getFullYear();
  let month = new Date().getMonth();
  let day = new Date().getDay();
  return `src/public/uploads/signer/poster/${year}/${month}/${day}`;
};

const SignerStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    let dir = GetDirectory();
    mkdirp(dir).then((made) => {
      cb(null, dir);
    });
  },
  filename: (req, file, cb) => {
    let fileName = GetDirectory() + "/" + file.originalname;
    cb(null, file.originalname);
  },
});

const UploadSigner = multer({
  storage: SignerStorage,
});

module.exports = UploadSigner;

But when I want to upload both files at the same time it show me this error :

MulterError: Unexpected field at wrappedFileFilter (F:\Projects\Nodejs\SalesSignal\node_modules\multer\index.js:40:19) at Busboy. (F:\Projects\Nodejs\SalesSignal\node_modules\multer\lib\make-middleware.js:114:7) at Busboy.emit (events.js:315:20) at Busboy.emit (F:\Projects\Nodejs\SalesSignal\node_modules\busboy\lib\main.js:38:33) at PartStream. (F:\Projects\Nodejs\SalesSignal\node_modules\busboy\lib\types\multipart.js:213:13) at PartStream.emit (events.js:315:20) at HeaderParser. (F:\Projects\Nodejs\SalesSignal\node_modules\dicer\lib\Dicer.js:51:16) at HeaderParser.emit (events.js:315:20) at SBMH.emit (events.js:315:20) at SBMH._sbmh_feed (F:\Projects\Nodejs\SalesSignal\node_modules\streamsearch\lib\sbmh.js:159:14) at SBMH.push (F:\Projects\Nodejs\SalesSignal\node_modules\streamsearch\lib\sbmh.js:56:14) at HeaderParser.push (F:\Projects\Nodejs\SalesSignal\node_modules\dicer\lib\HeaderParser.js:46:19) at Dicer._oninfo (F:\Projects\Nodejs\SalesSignal\node_modules\dicer\lib\Dicer.js:197:25) at SBMH. (F:\Projects\Nodejs\SalesSignal\node_modules\dicer\lib\Dicer.js:127:10)

whats the problem ? how can i solve this problem ???

1

1 Answers

1
votes

To handle multiple fields you cannot call the .single() middleware multiple times, instead you should use .array() or .fields(). Here's an example how you would use the latter:

app.post("/upload", upload.fields([
    {name: 'signerPoster'},
    {name: 'signerProfile'}
]), (req, res) => {
   // do something with req.files and req.body
});

The uploaded files will be populated under req.files.signerPoster and req.files.signerProfile and req.body will contain the text-based fields.