Alamofire:取消(暂停)文件上传

时间:2016-10-05 20:01:01

标签: ios alamofire

我正在使用Alamofire上传文件:

    Alamofire.Manager.upload(
        APIManager.Router.UploadFileRoute,
        multipartFormData : { multipartFormData in
            multipartFormData.appendBodyPart(data: audioData, name: "file", fileName: "file.m4a", mimeType: "audio/m4a")
        }, encodingCompletion: { result in
            switch result {
            case .Success(let request, _, _):
              break
            case .Failure(let encodingError):
              break
            }
    })

如何在文件上传开始后取消(暂停)该文件?

documentation建议我在Request对象上调用suspend(),但是如何使用上面的文件上传API获取Alamofire.Request对象呢?此API不会返回任何Alamofire.Request对象。

1 个答案:

答案 0 :(得分:1)

只有在编码后才会准备好请求(请参阅内联注释)

Alamofire.Manager.upload(
    APIManager.Router.UploadFileRoute,
    multipartFormData : { multipartFormData in
        multipartFormData.appendBodyPart(data: audioData, name: "file", fileName: "file.m4a", mimeType: "audio/m4a")
    }, encodingCompletion: { result in
        switch result {
        case .Success(let request, _, _):
          // Reference the request here (will get here immediately)
             request.responseJSON { response in
                  // This closure executes only after request succeeds or fails
             }
          break
        case .Failure(let encodingError):
          break
        }
})