我想将图像以多部分形式发送到服务器。正常
    图片上传与多部分一起工作,但面对
    问题是当我需要在字典中的数组中设置withname时。

  alamofireManager.upload(multipartFormData: { multipartFormData in

            for i in 0..<images.count {

                let imgData = UIImagePNGRepresentation(images[i])!

                    multipartFormData.append(imgData, withName: "fileUpload",fileName: "\(images)i", mimeType: "image/png")

            }


服务器参数就像

 "documants" : [{
    "documentType" : "Image",
    "fileUpload: "" // multipart data
},{
    "documentType" : "Image",
    "fileUpload: "" // multipart data
}]


那么,如何使用documents[0].fileUploadwithName中提到节点名称multipartFormData

最佳答案

您可以通过Swift4.2以这种方式实现“多部分”

let headers: HTTPHeaders = [
            /* "Authorization": "your_access_token",  in case you need authorization header */
            "Content-type": "multipart/form-data"
        ]

        let url = try! URLRequest(url: baseURL, method: .post, headers: headers)

        Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(img, withName: "file", fileName: imgName, mimeType: "\(fileType ?? "jpg")")
        }, with: url) {  result in
            switch result {
            case .success(let upload, _, _):
                upload.responseString { response in
 switch (response.response?.statusCode)
                    {
                        case 200: //The request was fulfilled
                            print("Network - HandShaking Successfull...!!!")
                            debugPrint(response)
                    }

            case .failure(let encodingError):
                print(encodingError)
            }
 }


希望您能了解“多部分” ... !!!

关于swift - 如何快速设置带有字典的多部分“withname”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55350215/

10-09 15:43