如何在 swift 中使用字典设置多部分"withname"?



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

那么如何使用multipartFormData提及节点名称documents[0].fileUpload withName呢?

你可以通过这种方式实现"多部分"...使用 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)
            }
 }

希望你能了解"多部分"...!!

最新更新