Multer: Upload multiple files with multer?

Created on 21 Aug 2018  路  8Comments  路  Source: expressjs/multer

I just can't figured whats wrong why my codes below. I try to upload multiple files. The problem is that i had an "UNEXPECTED FIELD"... I can make it to upload single file just fine, If I put upload.single('image') but I can't make it upload multiple files. So what's wrong?

html
<input id="file" type="file" class="form-control" placeholder="Imagen" name="image" multiple>

component.js

var payload = new FormData();
var files =  $('#file')[0].files

        for (key in $scope.producto) {
            payload.append(key, $scope.producto[key])
            console.log($scope.producto[key])
        }


     if (files.length > 0) {

            for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    console.log(file)
                    console.log(files.length)
                    payload.append('image', file);
                }
            }
       $http.post('http://localhost:5001/api/v1/articles', payload, {
            transformRequest: angular.identity,
            headers: { "Content-Type": undefined },
        }).then(function (response) {
            $location.path("/")
        })

And here 's the server part:
app.js

const multer = require('multer')
const storage = multer.diskStorage({
    destination : function(req, file, cb){
        cb(null, './uploads/');
    },
filename : function(req, file, cb){
    cb(null, new Date().toISOString() + file.originalname)
}

})

const upload = multer({storage : storage, limits: {
    fileSize : 1024 * 1024 * 5 
}})

.post('/api/v1/articles/', upload.array('image', 2 ), controllerArticle.postArticles)`

ControlletArticle.js

function postArticles(req, res) {
    // console.log(req.file)
if (req.file) {

const nvo = new articulos({
    name: req.body.name,
    price: req.body.price,
    condition: req.body.condition,
    brand: req.body.brand,
    productImage: req.file.path
})
nvo.save((err, productStored) => {
    if (err) res.status(500)

    res.status(200).send({ nvo: productStored })
})
}
}

modelarticle.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const articulos = Schema({
    name:  String,
    price: Number,
    condition: String,
    brand: String,
    productImage:  { type: String }
})

module.exports = mongoose.model('articulos', articulos)

I have tried so far:

Change upload.array() to upload.any(),and doesn't work.. any help? thanks!

Most helpful comment

Instead of upload.single('image') you want to do upload.array('image'), and then look at req.files instead of req.file.

All 8 comments

Instead of upload.single('image') you want to do upload.array('image'), and then look at req.files instead of req.file.

thanks LinusU, its work now

Instead of upload.single('image') you want to do upload.array('image'), and then look at req.files instead of req.file.

@LinusU so using upload.fields(fields) won't work?

Instead of upload.single('image') you want to do upload.array('image'), and then look at req.files instead of req.file.

Error: Too many files

This article is helpful by using with Multiple File Uploading on MVC Pattern in Express

Instead of upload.single('image') you want to do upload.array('image'), and then look at req.files instead of req.file.

What if I want to do .single() and .array() at the same time??

If you want to upload multiple files with one single field then you have to use upload.array('image', 2). Else if you want to upload files with multiple fields then you have to use upload.fields([{name:'image', maxCount: 1}, {name: 'banner',maxCount: 1}]) . You can access file by
req.files['image'][0]
req.files['banner'][0] as well
Hope that will help.

This article is helpful by using with Multiple File Uploading on MVC Pattern in Express

This article contains information to upload single file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tonghae picture tonghae  路  4Comments

arsinawaz picture arsinawaz  路  4Comments

BlueOctober picture BlueOctober  路  3Comments

edi picture edi  路  4Comments

trexanhvn picture trexanhvn  路  3Comments