Nodejs - 访问中间件对象--Multer-S3

时间:2017-01-23 01:52:50

标签: node.js express multer multer-s3

我正在使用multermulter-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

0 个答案:

没有答案