alamofire multipartformdata使用urlrequest
我想使用Alamo的fire multipart表单数据使用请求,例如我使用上传API
let profile = self.photoView.imageView?.image let parameters : [String:String] = [ "homePageUrl": webURLField.text!, "nickName": nickNameField.text!, "selfIntro": introField.text!, ] let uri = Constants.APIURL.changeProfile let fileName = "\(nickNameField.text!).jpg" Alamofire.upload(multipartFormData: { (multipartFormData) in if let imageData = UIImageJPEGRepresentation(profile!, 1.0) { multipartFormData.append(imageData, withName: "profile", fileName: fileName, mimeType: "image/jpg") } for ( key, value ) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key) } }, usingThreshold: UInt64.init(), to: uri, method: .patch, headers: Constants.VyrlAPIConstants.getHeader(), encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.uploadProgress(closure: { (progress) in }) upload.responseString { response in if ((response.response?.statusCode)! == 200){ self.navigationController?.popViewController(animated: true) } } case .failure(let encodingError): print(encodingError.localizedDescription) } })
这个代码从服务器日志发送400响应。
PATCH'http://blablabla.com:8080/users/profile':
内容types:multipart / form-data; border = alamofire.boundary.53a2e440bad1fabd X-Device:ios X-APP-Version:1.0.0 Accept-Language:ko-kr 400'http://blablabla.com:8080/users/profile'[0.0976 s]:X -Application-Context:application:dev连接:closuresTransfer-Encoding:Identitydate:2017年7月13日星期四01:57:41 GMT
服务器想要收到http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is ..服务器日志是..(成功200代码)
curl -X PATCH –header'Content-Type:multipart / form-data'–header'Accept: / '–header'X-APP-Version:1.0.0'–header'X-Device:ios' –header'Accept-Language:ko-KR'{“type”:“formData”}' http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is .. '
绝对,我想附加?nicName = abcd使用multiformpart数据
如何使用alamofire代码?
如果不工作,那么使用这样的参数做url。
Alamofire.upload(multipartFormData:{ multipartFormData in multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")}, usingThreshold:UInt64.init(), to:uri + "?" +nickNameField.text! +"?"+ webURLField.text!+"?"+introField.text!, method:.post, headers:Constants.VyrlAPIConstants.getHeader(), encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in print(response.result.value) } case .failure(let encodingError): print(encodingError) } })
请尝试这是它为我工作的图像上传
var parameters = [String:AnyObject]() let profile = self.photoView.imageView?.image parameters = ["homePageUrl": webURLField.text as AnyObject, "nickName": nickNameField.text as AnyObject, "selfIntro": introField.text as AnyObject] let imgData = UIImageJPEGRepresentation(profile!, 0.2)! let uri = Constants.APIURL.changeProfile let fileName = "\(nickNameField.text!).jpg" Alamofire.upload(multipartFormData:{ multipartFormData in multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg") for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } }, usingThreshold:UInt64.init(), to:uri, method:.post, headers:Constants.VyrlAPIConstants.getHeader(), encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in print(response.result.value) } case .failure(let encodingError): print(encodingError) } })
Swift 3 Alamofire 4
这是参考的演示代码
Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append(UIImageJPEGRepresentation(self.Imgprofile.image!, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg") }, to:" Your URL here ") { (result) in switch result { case .success(let upload, _, _): print(result) upload.uploadProgress(closure: { (progress) in print(progress) }) upload.responseJSON { response in //print response.result print(response); } case .failure(let encodingError): print(encodingError); } } }
希望这应该工作
谢谢
- 上传IPA,奇怪的问题:错误ITMS-90032:“无效的图像path – 在键'$ key'下引用的path没有find图像:'$ path'
- 快速恢复Alamofire在超时/丢失连接后上传
- iOS上传.pdf文件
- iOS上的Twitter状态/ update_with_media返回500错误
- 上传IOS应用程序到应用程序商店。 似乎冻结和互联网停止工作
- 错误ITMS-9000:“缺less代码签名权利。 在捆绑中找不到权利“ – 如何更改应用程序ID名称
- POST SQLITE文件到远程服务器
- Youtube APIauthentication – iPhone
- iOS如何通过stream式传输将大型资产file upload到服务器