节点服务器Multer req.files为空

时间:2018-06-26 00:08:11

标签: node.js multipartform-data multer alamofireimage

我一直在尝试通过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");
    });

});

1 个答案:

答案 0 :(得分:0)

能够解决此问题,却错过了

fileName: "photo.jpg"

此行中的属性:

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