我通过Alamofire.upload作为多部分数据在服务器上上传图像.与Alamofire.request不同,它不返回Request对象,我通常用它来取消请求.
但是能够取消像上传这样的消费请求是非常合理的. Alamofire有哪些选择?
使用Alamofire自述文件中的
Uploading MultiPartFormData示例:
Alamofire.upload( .POST,"https://httpbin.org/post",multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: unicornImageURL,name: "unicorn") multipartFormData.appendBodyPart(fileURL: rainbowImageURL,name: "rainbow") },encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload,_,_): upload.responseJSON { response in debugPrint(response) } case .Failure(let encodingError): print(encodingError) } } )
在这里,upload.responseJSON返回一个Request,它允许你将它分配给某些东西以便以后取消.例如:
let request = upload.responseJSON { ... ... request.cancel()