1
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);

以上是multer s3代码

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);   });

以上代码调用multer上传功能。我正在将 API 中的数据(来自 Angular2,设置 -Content-Type”:“multipart/form-data)解析为 formdata

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

我需要req.body来自 API 的数据,例如文件夹名称和其他数据,因此我可以将文件放在 S3 上的特定位置。在 multerS3 的 key: 中需要 req.body 数据function(req, file, cb) {

4

2 回答 2

0

这似乎是 multer 中的一个错误,但正如这里所说,您可以在发送时重新排序参数。

https://github.com/expressjs/multer/issues/322#issuecomment-191642374

于 2021-08-24T08:46:56.740 回答
-1

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

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

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

所以像这样传递id:new FormData(document.getElementById(myForm)

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

于 2018-07-05T12:53:24.177 回答