如何在字典中快速设置多部分“ withname”?

时间:2019-03-26 05:09:39

标签: swift

我想以多部分形式将图像发送到服务器。正常     图片上传功能适用于多部分内容,但面向     问题是当我需要在字典中使用数组设置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

1 个答案:

答案 0 :(得分:0)

您可以通过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)
            }
 }

希望您会了解'multipart'... !!!