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

Popular posts from this blog

How To Migrate MVC 3 Application To MVC 5

Private Chat Using Node js and socket.io with encrypt and decrypt message and insert into mongo db

Populate a drop-down in Vue.js and Asp.net Core from an ajax call