【问题标题】:Node server Multer req.files is empty节点服务器 Multer req.files 为空
【发布时间】:2018-12-04 14:35:26
【问题描述】:

我一直在尝试通过 multer 数组方法将几张图像上传到我的 Node js 服务器,并且我已经阅读并尝试了大约 20 种不同的方法,但都没有成功。我正在使用 Alamofire .upload 方法通过 iOS 上传,该方法为 http 正文生成 multipart/form-data。我能够成功上传单张图片,但不能上传数组。

iOS 部分

Alamofire.upload(
        multipartFormData: { multipartFormData in
            for img in images {
                guard let imageData = UIImageJPEGRepresentation(img, 0.25) else { 
                    return 
                }
                multipartFormData.append(imageData, withName: "photo")
            }
    },

route.js

var upload = multer({
    dest: './jobs',
    limits: (40 * 1024 * 1024), // (MB * kb * bytes)
    storage: multer.memoryStorage()
});

router.post("/xyz/:id/upload", upload.array('photo'), function (req, res) { //  upload.single('photo')
    console.log("POST to job photo upload route \n");
    const mDy = new Date().toLocaleDateString(format, dateOptions);
    var filePath = String("xyz/" + req.params.id + " " + mDy + ".jpg");
    var writeStream = fs.createWriteStream(filePath);

    req.pipe(writeStream);
    console.log(req.files);

    req.on('end', () => {
        res.send(201, "photos saved");
        console.log("photos saved");
    });

});

【问题讨论】:

  • 因此使用upload.array('photo'),我能够在req.body 中获取数据,但它似乎不是数组形式。

标签: node.js multipartform-data multer alamofireimage


【解决方案1】:

能够解决这个问题,一直错过

fileName: "photo.jpg"

这一行的属性:

multipartFormData.append(imageData, fileName: "photo.jpg", withName: "photo")

【讨论】:

  • 谢谢!这在将近 2 年后有所帮助
猜你喜欢
  • 1970-01-01
  • 2018-12-21
  • 2016-10-30
  • 2019-02-06
  • 2016-09-05
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 2019-01-05
相关资源
最近更新 更多