阿拉莫火!在多部分文件上传中,progress.isPausable 返回 false,progress.pause() 不起作用
Posted
技术标签:
【中文标题】阿拉莫火!在多部分文件上传中,progress.isPausable 返回 false,progress.pause() 不起作用【英文标题】:Alamofire! In multipart file upload progress.isPausable returns false, and progress.pause() does not work 【发布时间】:2017-11-02 07:01:53 【问题描述】:我想上传具有暂停和恢复功能的文件,只有上传才能正常工作!
代码如下!
configuration = URLSessionConfiguration.background(withIdentifier:
"com.example.app.background")
sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.upload( // Or Alamofire.upload(
multipartFormData: multipartFormData in
multipartFormData.append(self.videoURL, withName: "file", fileName:
fileName, mimeType: "video/(extention)")
,
to: myServer,
encodingCompletion: encodingResult in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
if response.error == nil
self.bar_progress.progress = 1
self.lbl_uploadProgress.text = "Upload Progress: 100%"
print("Upload Status Success: ", response)
else
print("Upload Status Fail: ", response)
upload.uploadProgress progress in
print("Progress: ", progress.fractionCompleted)
self.bar_progress.progress = Float(progress.fractionCompleted)
self.lbl_uploadProgress.text = "Upload Progress: \( Int(progress.fractionCompleted * 100))%"
if progress.isPausable
print("Pausable") // THIS IS WHAT I WANT
else
print("Not pausable") // THIS IS MY PROBLEM
case .failure(let encodingError):
print(encodingError)
self.bar_progress.progress = 0
self.lbl_uploadProgress.text = "Upload Progress: 0%"
)
谢谢,提前。
【问题讨论】:
【参考方案1】:您需要计算上传文件的进度。如果进度小于 100%,那么你保持你的视图暂停,一旦它 100%,你可以像这样恢复它。
Alamofire.upload(
.POST,
URLString: "http://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.progress bytesRead, totalBytesRead, totalBytesExpectedToRead in
let progress: Float = Float(totalBytesRead)/Float(totalBytesExpectedToRead) // you can give this progress to progressbar progress
let value = Int(progress * 100) // this is the percentage of the video uploading
if value == 100
// resume the view
else
// keep it pause
upload.responseJSON request, response, result in
debugPrint(result)
case .Failure(let encodingError):
print(encodingError)
)
【讨论】:
以上是关于阿拉莫火!在多部分文件上传中,progress.isPausable 返回 false,progress.pause() 不起作用的主要内容,如果未能解决你的问题,请参考以下文章