我在Home VC中用Alamofire上传了一个文件。而是在类函数中上传函数。所以我没有得到进度操作。我想我必须要写委托方法。
有人愿意帮我吗?
我的问题在ViewController中!
SendPhoto.class中的
class SendPhoto {
func send() {
Alamofire.upload(
multipartFormData: { multipartFormData in
...
},
to: "",
headers:headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress {progress in
print(progress.fractionCompleted)
}
upload.responseJSON { response in
}
case .failure(let encodingError):
print("File Upload Error")
print(encodingError)
}
})
}}
ViewController中的
class HomepageVC: UIViewController .. {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendAction(_ sender: Any) {
SendPhoto().send()
// I want to keep this function progress and print to a label or etc. !!!
}
}
发布于 2018-05-29 14:23:51
然而,你的问题并没有完成,所以是假设的。
Alamofire在除多部分请求之外的任何上载请求上返回UploadRequest对象
并且upload请求具有属性进度
/// The progress of fetching the response data from the server for the request.
open var progress: Progress { return dataDelegate.progress }
它可以用来跟踪上传的进度
编辑
您必须将带有闭包的数据传递给视图控制器
这是你的方法
private func uploadAnyThing (progress:@escaping ((Progress) -> Void) , completed:@escaping ((_ success:Bool,_ error:Error?) -> Void)) {
Alamofire.upload(multipartFormData: { (data) in
}, to: "test.com") { (result) in
switch (result) {
case .success(let request, _, _):
request.uploadProgress(closure: { (prog) in
progress(prog)
})
request.responseJSON(completionHandler: { (res) in
completed(true,nil)
})
case .failure(let error) :
completed(false,error)
break
}
}
}
你可以通过以下方式获取
func test () {
uploadAnyThing(progress: { (progres) in
print(progres.fractionCompleted)
}) { (success, error) in
}
}
https://stackoverflow.com/questions/50577405
复制相似问题