I want to upload a file with pause and resume functionality, Only uploading works fine!
The code is below!
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%"
}
}
)
Thanks, in advance.
You need to calculate the progress of uploading file. If progress is less than 100% then you keep your view pause and once its 100% you can resume it just like this.
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)
}
}
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.