Tuesday, 11 April 2017

Multer not uploading the file

I have been using angular fullstack for my project and when I use multer nothing happens.The file isnt saved in the directory and no error is thrown.Below is the piece of code I used. HTML:

<input type="file" file-model="myFile"/><br><br>

Controller:

$scope.postAdd=function(){
       var file = $scope.myFile;
        var fd = new FormData();
        fd.append('file', file);
        console.log(file);
        $http.post('/api/vLibs',fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
          console.log("success!!");
        })
        .error(function(){
          console.log("error!!");
          alert('File upload failed')
        });
       }

Api's index.js:

router.post('/', upload.single('file'), controller.create);

API:

var multer  = require('multer');
 var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/');
        console.log("here");
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname+ '-' + Date.now()+'.jpg')
    }
});
  var upload = multer({ storage: storage });



via akshay

No comments:

Post a Comment