Upload Image using Node js
Server.js
var multer = require('multer');
app.post('/api/photo', function (req, res) {
try {
imagedata(req, res, function (err) {
if (err) {
var response = {
"success": "0"
, data: {
"msg": "Error uploading file."
}
}
res.end(response);
}
else {
console.log("File Upload Successfully");
var response = {
"success": "1"
, data: {
"msg": "file Upload Success fully"
}
}
res.end(response);
}
});
}catch (err) {
var response = {
"success": "0"
, data: {
"msg": "Error Handle in Catch" + err
}
}
res.json(response);
}
})
var imagedata = multer({
storage: storage
}).array('Photo', 20); // key is Photo And 20 is no.of file can accept at a time
var storage = multer.diskStorage({
destination: function (req, file, callback){
callback(null, './uploads/images');
},
filename: function (req, file, callback){
var imagname = file.originalname;
var ext = []
ext = imagname.split('.');
callback(null, Date.now()+ '.' + ext[ext.length - 1]);
}
Comments
Post a Comment