简体   繁体   English

Alamofire! 在分段文件上传中,progress.isPausable返回false,并且progress.pause()不起作用

[英]Alamofire! In multipart file upload progress.isPausable returns false, and progress.pause() does not work

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. 如果进度小于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)
    }
}

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM