我设法使用multipart-form-data Alamofire上传文件:
Alamofire.upload(.POST, "api.myservice.com", headers: myheaders, multipartFormData: { (multipartFormData:MultipartFormData) -> Void in multipartFormData.appendBodyPart(data: json, name: "metadata", mimeType: "application/json") multipartFormData.appendBodyPart(data: self.data, name: "document", fileName: "photo.png", mimeType: "image/png") }, encodingMemoryThreshold: 10 * 1024 * 1024) { (result:Manager.MultipartFormDataEncodingResult) -> Void in }
但我看不到跟踪上传进度的方法,并且在上传完成(或失败)后调用完成块.有没有办法在Alamofire做到这一点?
注意:我知道可以上传进度,但我正在研究多部分表单数据.
这是一种完成,失败和进度关闭的方法(感谢我的同事指出解决方案):
Alamofire.upload(.POST, absPath(), headers: headers(), multipartFormData: { (multipartFormData:MultipartFormData) -> Void in multipartFormData.appendBodyPart(data: json, name: "metadata", mimeType: "application/json") multipartFormData.appendBodyPart(data: self.data, name: "document", fileName: "photo.png", mimeType: "image/png") }, encodingMemoryThreshold: 10 * 1024 * 1024, encodingCompletion: { (encodingResult) -> Void in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { response in // success block } upload.progress { _, totalBytesRead, totalBytesExpectedToRead in let progress = Float(totalBytesRead)/Float(totalBytesExpectedToRead) // progress block } case .Failure(_): // failure block } })