I am using the multer
and multer-s3
modules to bulk upload multiple files, but after the files are uploaded, I would like to access the newly imported files to then be used within my page, but I can't seem to figure out the best way to access the object that would contain the information related to these files. Should I set the cb
to the req
object?
Here is my current setup:
var upload = multer({
storage: multerS3({
s3: s3,
bucket: options.Bucket,
contentType: multerS3.AUTO_CONTENT_TYPE,
acl: options.ACL,
key: function(req, file, cb){
var fileNameFormatted = file.originalname.replace(/\s+/g, '-').toLowerCase();
cb(null, req.user.organizationId + '/' + uploadDate + '/' + fileNameFormatted);
req.data = cb;
}
})
});
appRoutes.post('/sign', upload.array('fileUpload', 5), function(req, res){
console.log('This is a upload ' + JSON.stringify(req.data));
});
File Input:
<input type="file" id="file-input" name="fileUpload[]" multiple>
jQuery triggering route:
$("#file-input").on('change', function(){
var files = $(this).get(0).files;
if (files.length > 0){
var formData = new FormData();
for(var i = 0; i < files.length; i++){
var file = files[i];
console.log(files[i]);
formData.append('fileUpload', file, file.name);
}
$.ajax({
url: '/sign',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data){
console.log('upload successful! ' + data);
},
error: function(error){
console.log('error ' + JSON.stringify(error));
}
});
}
});
req.data
returns undefined