I have been able to set up a process to upload a single image at a time using NodeJS/Express/Amazon S3/ Multer. It works perfectly. I've been trying to change the code to allow users to upload more than one image at a time. So far I have been very unsuccessful. How would I change my code below to allow multiple images to be uploaded at once? Thanks!
aws.config.update({
secretAccessKey: '*****************',
accessKeyId: '******',
region: 'us-east-2'
});
var s3 = new aws.S3();
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'myfiles',
key: function (req, file, cb) {
var fileExtension = file.originalname.split(".")[1];
var path = "uploads/" + req.user._id + Date.now() + "." + fileExtension;
cb(null, path);
}
})
});
router.post("/", upload.array('image', 1), function(req, res, next){
var filepath = undefined;
if(req.files[0]) {
filepath = req.files[0].key;
}......
you have done the hard part, all what u have to do is to modify your html file input to make it accept multiple files like so
<input type="file" name="img" multiple>
and change the number of files in the array to the maximum number of files you wan to upload
from
upload.array('image', 1)
to
upload.array('image', x)
where (x) is the maximum number of files per upload
EDIT1 : update
Here is kind of full example & to avoid "too large entity issue"
var express = require("express");
var app = express();
var multer = require('multer');
var cookieParser = require('cookie-parser');
var path = require('path');
var router = express.Router();
app.use("/", router);
app.use(bodyParser.json({limit: "50mb"}));
app.use(cookieParser());
var urlencodedParser = bodyParser.urlencoded({
extended: true,
parameterLimit: 50000
});
// in case u want to c the requsted url
router.use(function(req, res, next) {
console.log('Request URL: ', req.originalUrl);
next();
});
//the files will b uploaded to folder name uploads, html file input name is uploadedFile
app.post('/your/route', urlencodedParser, function(req, res) {
var storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, './uploads');
},
filename: function(req, file, callback) {
var fname = file.fieldname + '-' + Date.now() + path.extname(file.originalname);
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
var upload_photos = multer({
storage: storage
}).array('uploadedFile', 3);
upload_photos(req, res, function(err) {
// uploading files
});
});