问题描述
我正在通过 Alamofire.upload
在服务器上上传图像作为多部分数据。与 Alamofire.request
不同,它不会返回 Request
对象,我通常使用该对象来取消请求。
I am uploading images on server via Alamofire.upload
as multipart data. Unlike Alamofire.request
it's not returning Request
object, which I usually use to cancel requests.
但是取消诸如上传之类的消耗性请求是非常合理的。
But it's very reasonable to be able to cancel such a consuming requests like uploading. What are the options for this in Alamofire?
推荐答案
使用示例(自述文件):
Using the Uploading MultiPartFormData example from the Alamofire README:
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
返回一个请求
,该请求应允许您将其分配给某些内容以供以后取消。例如:
Here, upload.responseJSON
returns a Request
, which should allow you to assign it to something for cancellation later. For example:
let request = upload.responseJSON { ...
...
request.cancel()
这篇关于如何取消Alamofire.upload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!