简体   繁体   中英

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

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...'}) } }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM