Gorge
Gorge

Reputation: 21

Combining angular-file-upload and multer

I have big headache to combine angular file upload plugin with multer to make it fully SPA. I stucked on uploading multiple files through multer.

This is how my multer options looks like: (node route.js file)

var upload = multer({
    storage: storage,
    limits: {
        //fileSize: 819200
    }
}).array('myFile');

this is my POST: (node route.js file)

router.post('/add/file', function(req, res, next) {
    upload(req,res,function(err) {
        console.log(req.files);
        if(err) {
            console.log("Error uploading file.");
        }
    });
});

this is inside my angular controller:

var uploader = $scope.uploader = new FileUploader({
    url: 'http://localhost:3000/add/file',
    alias: 'myFile'
});

uploader.filters.push({
    name: 'imageFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
        var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
        return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    }
});

It adds only 1st file and stucks - I don't get any error it just stucks - whole page works and I can send files again, but again only 1st file will be uploaded. Console shows that req.files have only 1 file (that first one)

I couldn't find any tutorial or anything on the Internet with angular-file-upload plugin, that's why I ask you guys

Upvotes: 0

Views: 504

Answers (1)

PandaScript
PandaScript

Reputation: 55

Not sure if you figured this out yet or not, but with sending multiple files over, the 'uploadAll' function will not send the next file until it receives a response back from the server. So the route should look like this. I also saw somewhere in the documentation that the response needs to be json...haven't tested whether or not this is true though

router.post('/add/file', function(req, res, next) {
    upload(req,res,function(err) {
        console.log(req.files);
        if(err) {
            console.log("Error uploading file.");
        } else {
        res.status(200).json({response: 'some response...'})
       }
    });
});

Upvotes: 1

Related Questions