multer array upload does not upload files, getting an empty array

717 Views Asked by At

Want to upload an array of 3 files, but in my nodejs i get only an empty array from the angular controller.

In my angular controller i get the files from html with an onchange method, and send it with an http request to the upload function in my nodejs backend.

this.selectFile1 = function(files){
           fileArray[0] = files[0];
           self.files = files;
       }

       this.selectFile2 = function(files){
           fileArray[1] = files[0];
           self.files = files;
       }
       this.selectFile3 = function(files){
           fileArray[2]= files[0];
           self.files = files;
       }

this.upload = function(){
             var fd = new FormData();
             var file = fileArray;
             fd.append('file', file);

         $http.post("/upload", fd, {
             transformRequest: angular.identity,
             headers: {"Content-Type": undefined}
         }).then(function success(response){
             if(response.status == 200){
                 console.log('success');
             }
         }, function error(response){
             console.log(response.data.msgUpload);
         })
     }

Nodejs upload function:

var storage =   multer.diskStorage({
           destination: function (req, file, cb) {
             cb(null, '../uploads');
           }
        });

var upload = multer({ storage : storage});
router.post('/upload', upload.array('file', 3), function(req,res, next){
    console.log(req.files);
        if(!req.files){
            res.status(400);
        }else{
        res.status(200);
        }
});

why do I get an empty array? and how can I insert the files which are to be uploaded?

0

There are 0 best solutions below