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 }