前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS Alamofire上传文件到服务器

IOS Alamofire上传文件到服务器

作者头像
用户5760343
发布2019-07-08 11:59:45
3.6K0
发布2019-07-08 11:59:45
举报
文章被收录于专栏:sktj
代码语言:javascript
复制
 103 @IBAction func
 touchupInsideUploadingFileBtnAction(_ sender:AnyObject)
 {
 104 let fileURL =
 Bundle.main.url(forResource:“tortoise”,
 withExtension:“png”)
 105
 Alamofire.upload(fileURL!,to:“https://httpbin.org/post”)
 106 .validate()
 107 .responseJSON { response in
 108 DispatchQueue.main.async{
 109 print(“上传结果:(response.result)”)
 110 let message = “上传结果:(response.result)”
 111
 112 let alert = UIAlertController(title:“Information”,
 message:message, preferredStyle:
 UIAlertControllerStyle.alert)
 113 let OKAction = UIAlertAction(title:“OK”, style:
 UIAlertActionStyle.default, handler:nil)
 114 alert.addAction(OKAction)
 115 self.present(alert, animated:true, completion:nil)
 116 }
 117 }
 118 }
 119 public func upload(
 120 _ fileURL:URL,
 121 to url:URLConvertible,
 122 method:HTTPMethod = .post,
 123 headers:HTTPHeaders?= nil)
 124 -> UploadRequest
 125 {
 126 return SessionManager.default.upload(fileURL, to:
 url, method:method, headers:headers)
 127 }
 128 @IBAction func
 touchupInsideUploadingFileWithProgressBtnAction(_
 sender:AnyObject) {
 129 let fileURL =
 Bundle.main.url(forResource:“tortoise”,
 withExtension:“png”)
 130
 Alamofire.upload(fileURL!,to:“https://httpbin.org/post”)
 131 .uploadProgress { progress in
 132
 133 print(“完成比例:(progress.fractionCompleted)”)
 134 print(“当前完成:(progress.completedUnitCount)”)
 135 print(“总共大小:(progress.totalUnitCount)”)
 136 }
 137 .validate()
 138 .responseJSON { response in
 139 DispatchQueue.main.async{
 140 print(“上传结果:(response.result)”)
 141 let message = “上传结果:(response.result)”
 142
 143 let alert = UIAlertController(title:“Information”,
 message:message, preferredStyle:
 UIAlertControllerStyle.alert)
 144 let OKAction = UIAlertAction(title:“OK”, style:
 UIAlertActionStyle.default, handler:nil)
 145 alert.addAction(OKAction)
 146 self.present(alert, animated:true, completion:nil)
 147 }
 148 }
 149 }
 150 open func uploadProgress(queue:DispatchQueue =
 DispatchQueue.main, closure:@escaping ProgressHandler) ->
 Self {
 151 uploadDelegate.uploadProgressHandler = (closure,
 queue)
 152 return self
 153 }
//上传多个文件到服务器
 154 @IBAction func
 touchupInsideUploadingFileWithMultiPartBtnAction(_
 sender:AnyObject) {
 155 let unicornImageURL =
 Bundle.main.url(forResource:“tortoise”,
 withExtension:“png”)
 156 let rainbowImageURL =
 Bundle.main.url(forResource:“tortoise”,
 withExtension:“png”)
 157 Alamofire.upload(
 158 multipartFormData:{ multipartFormData in
 159 multipartFormData.append(unicornImageURL!,
 withName:“unicorn”)
 160 multipartFormData.append(rainbowImageURL!,
 withName:“rainbow”)
 161 } ,to:“https://httpbin.org/post”,
 162 encodingCompletion:{ encodingResult in
 163 switch encodingResult {
 164 case .success(let upload, _, _):
 165 upload.responseJSON { response in
 166 debugPrint(response)
 167 }
 168 case .failure(let encodingError):
 169 print(encodingError)
 170 }
 171 }
 172 )
 173 }
 174 public func upload(
 175 method:Method,
 176 _ URLString:URLStringConvertible,
 177 headers:[String:String]?= nil,
 178 multipartFormData:MultipartFormData -> Void,
 179 encodingMemoryThreshold:UInt64 =
 Manager.MultipartFormDataEncodingMemoryThreshold,
 180 encodingCompletion:
 (Manager.MultipartFormDataEncodingResult -> Void)?)
 181 {
 182 return Manager.sharedInstance.upload(
 183 method,
 184 URLString,
 185 headers:headers,
 186 multipartFormData:multipartFormData,
 187 encodingMemoryThreshold:
 encodingMemoryThreshold,
 188 encodingCompletion:encodingCompletion
 189 )
 190 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档