我正在使用multer
andmulter-s3
模块批量上传多个文件,但在文件上传后,我想访问新导入的文件,然后在我的页面中使用,但我似乎无法找出访问的最佳方式将包含与这些文件相关的信息的对象。我应该将 设置cb
为req
对象吗?
这是我当前的设置:
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));
});
文件输入:
<input type="file" id="file-input" name="fileUpload[]" multiple>
jQuery触发路线:
$("#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
返回undefined