I am trying to upload a file in two location, it is working find when I am testing it from postman. But when we trying to upload file from my mobile app without it return an error. one interesting thing: when after restarting server if we first hit from postman then it is working from mobile app as well but in that case dynamic value to not updating, Value came from postman hit is used in dynamic field.
// app.get('/empid/:id', (req, res) => {
app.post('/upload/:id', function(req,res){
pool.query('SELECT first_name, username FROM user_detail where user_detail_pk=?', [req.params.id], (err, rows, fields) => {
if (!err)
res.send(rows);
else
console.log(err);
rows.forEach( (row) => {
console.log(`${row.first_name}`);
var fname = (`${row.first_name}`);
var username = (`${row.username}`);
console.log('First Name is : ' + fname);
console.log('UserName Name is : ' + username);
var dateObj = new Date();
//var month = dateObj.getUTCMonth() + 1; //months from 1-12
var year = dateObj.getUTCFullYear();
var month = dateObj.toLocaleString('default', { month: 'long' });
var day = dateObj.getUTCDate();
datewise = "datewise" + "/" + year + "/" + month + "/" + day + "/" + fname + "(" + username + ")";
empwise = "empwise" + "/" + fname + "(" + username + ")" + "/" + year + "/" + month + "/" + day;
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
let Id = req.body.id;
let datewisepath = `/home/KunDan/RestAPIUpload/Conveyance/${datewise}`;
let empwisepath = `/home/KunDan/RestAPIUpload/Conveyance/${empwise}`;
fs.mkdirsSync(datewisepath);
fs.mkdirsSync(empwisepath);
cb(null, datewisepath);
cb(null, empwisepath);
// callback(null, 'uploads/' + req.user.id);
},
/* filename: function (req, file, callback) {
callback(null, file.originalname + '-' + Date.now());
}
});
*/
filename: function (req, file, cb) {
console.log(file);
let extArray = file.mimetype.split("/");
let extension = extArray[extArray.length - 1];
cb(null, file.originalname + '-' + Date.now() + "." + extension);
console.log(req.file);
}
})
var upload = multer({ storage : storage }).array('dataFile',50);
upload(req,res,function(err) {
console.log(req.body);
console.log(req.files);
if(err) {
return res.end("Error uploading file.");
}
//return res.end("File is uploaded");
else
("File is uploaded");
});
}); });