I have created class named FileUpload and made one function to upload multiple files using multer.I want to use this method in controller but i cannot to do so. i can not get other fields from request. Here is the FileUpload class :
var multer = require('multer');
class FileUpload{
constructor(){
this.storage = null;
this.filepath = null;
this.upload = null;
}
uploadMultipleFile(req,res,path){
this.filepath = path;
this.storage = multer.diskStorage({
destination : (req,file,callback) =>{
callback(null,path)
},
filename : (req,file,callback)=>{
this.filepath = this.filepath + file.fieldname + '-' + new Date().getTime();
callback(null,this.filepath);
}
});
this.upload = multer({storage:this.storage}).array('files',req.files.length);
this.upload(req,res,(err) => {
if(err){
return res.status(403).send({
success:false,
message : SystemMessage.UploadErrorMessage.replace('{0}',"Files"),
data : {
filepath : filepath
}
});
}
return res.status(200).send({
success:true,
message : SystemMessage.UploadSuccessMessage.replace('{0}',"Files"),
data : {
filepath : filepath
}
});
});
}
}
module.exports = FileUpload;
Here is the controller file in which i have defined route:
const express = require('express');
const router = express.Router();
const FileUpload = require('../services/fileUpload');
router.post("/add",(req,res)=>{
let localdate=CommonFunction.datetime();
let fileUpload = new FileUpload();
let obj = {
user_id:req.body.user_id,
subject:req.body.subject,
message:req.body.message,
created_date:localdate,
modified_date:localdate
};
});
when i call this route from postman,i use form-data format in body.i got following response :
{
user_id: undefined,
subject: undefined,
message: undefined,
created_date: '2019-1-22 13:55:42',
modified_date: '2019-1-22 13:55:42'
}
Guide me how can i use uploadMultipleFile function in route /add ?.