我正在使用Alamofire 4 with swift 3
更新用户个人资料.我也在Router
上课.我需要的是uplaod
和其他参数一起成像.我可以向update
用户详细介绍,无需上传图像部分.
这就是它的样子 postman
所以有可能为此创建一个urlconvertible请求.如何使用其他参数上传图像.(这在邮递员中很好用).我怎么能用新的呢Alamofire
.我尝试过如下.
let parameters = [ "profile_image": "swift_file.jpeg" ] Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png") for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } }, to:urltoUpdate) { (result) in switch result { case .success(let upload, _, _): print("the status code is :") upload.uploadProgress(closure: { (progress) in print("something") }) upload.responseJSON { response in print("the resopnse code is : \(response.response?.statusCode)") print("the response is : \(response)") } break case .failure(let encodingError): print("the error is : \(encodingError.localizedDescription)") break } }
但这不能正常工作.希望你对这部分的帮助.
你不需要这个:
"profile_image": "swift_file.jpeg"
参数应该是:
let parameters = [ "firstname": "Bill", "surname": "fox", //...rest of the parameters ]
这个withName: "file"
:
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png")
应该是withName: "profile_image"
:
multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png")
带标题的代码:
let parameters = [ "firstname": "Bill", "surname": "fox", //...rest of the parameters ] let headers = [ "somekey": "somevalue", //...rest of the parameters ] Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png") for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } }, usingThreshold:UInt64.init(), to: "", //URL Here method: .post, headers: headers, //pass header dictionary here encodingCompletion: { (result) in switch result { case .success(let upload, _, _): print("the status code is :") upload.uploadProgress(closure: { (progress) in print("something") }) upload.responseJSON { response in print("the resopnse code is : \(response.response?.statusCode)") print("the response is : \(response)") } break case .failure(let encodingError): print("the error is : \(encodingError.localizedDescription)") break } })