How to get req.body parameters in multer s3

时间:2017-07-12 07:57:56

标签: node.js multer-s3

var upload = multer({
storage: multerS3({
    s3: s3,
    bucket: 'bucket',
    metadata: function(req, file, cb) {
        cb(null, {
            fieldName: file.fieldname
        });
    },
    key: function(req, file, cb) {
        console.log('req.body', req.params.id); //not getting
        console.log('req.body', req.body);
        //Not getting param here that passed in api 
        //Need to save file on s3 at specific location i.e /foldername/filename
        //But the folder name not getting from API
        cb(null, file.originalname)
    }
})  }).array('userFile', 1);

Above is multer s3 code

app.post('/saveData', function(req, res, next) {
upload(req, res, function(err) {
    console.log('err' + err);
    var status = '';
    var result = '';
    var link = '';
    if (err) {
        status = false;
    } else {
        status = true;
    }
    result = {
        "status": status,
        "link": link
    }
});
res.send(result);   });

Above code where calling multer upload function. I am parsing data in API (from Angular2, set -Content-Type": "multipart/form-data) as formdata

let formData: FormData = new FormData();
formData.append('userFile', file);
formData.append('fileName', fileName);

I require the req.body data from API like folder name and other, so I can put the file to specific place on S3. Need req.body data inside the key: function(req, file, cb) { of multerS3.

1 个答案:

答案 0 :(得分:-1)

阅读dropzone.js的源代码后,我发现问题在于S3希望文件是您的formdata的最后一个参数

如果文件输入是表单的最后输入,则在创建FormData()的新实例时也应尝试传递form标记的引用

<form
    id="myForm"
    className="col-md-10 col-md-offset-1"
>

这样传递ID:new FormData(document.getElementById(myForm)

那么您应该在关键的回调函数req.body中找到表格的输入值