如何在 Alamofire 中使用 multipart 在多个图像数组中上传多个图像?

How to upload multiple images in multiple array of images using multipart in Alamofire?

我在使用 Alamofire 中的多部分上传多个图像数组中的多个图像时遇到问题。 谁能帮我?提前致谢!!

我正在使用以下代码

let fileParameters = ["array_1": images1Array,
                      "array_2": images2Array]

func requestUpload(files: [String: Any]? = nil) {

Alamofire.upload( multipartFormData: { multipartFormData in
        if let files = files {
            for (key, value) in files {
                if let images = value as? [UIImage] {
                    for (index, image) in images.enumerated() {
                        let imageData = UIImageJPEGRepresentation(image, 1.0)
                        multipartFormData.append(imageData!, withName: "\(index)", fileName: "\(index).jpg", mimeType: "image/jpg")
                    }
                }
            }
        }
}, to: path , method: .post , headers: request.headers,
          encodingCompletion: { encodingResult in

            self.validatedData(of: encodingResult, handler: { (result, error) in
                handler(result, error)
            })
        })
}

所以我想知道如何在特定键中附加多个图像。

服务器请求是这样的:

"array1":[
     {
        product_image: img1.jpg
     },
     {
        product_image: img2.jpg
     }
  ],
  "array2":[
     {
        product_image: img3.jpg
     }
  ]

我希望这会奏效

使您的键数组类型:-

 multipartFormData.append(imageData!, withName: "product_image[\(index)]", fileName: "\(index).jpg", mimeType: "image/jpg")

创建循环以递增顺序创建索引值,例如 0,1,2,3.....N

使用平面图加入 imagesArrays 并循环遍历图像。

let allImages = fileParameters.values.flatMap{[=10=]}

allImages.enumerated().forEach {    
   multipartFormData.append(UIImagePNGRepresentation(.values.first), withName: "product_image\([=10=])", fileName: "\([=10=]).jpg", mimeType: "image/jpg")
}