dean
dean

Reputation: 331

Why does the uploadprogress for Alamofire never reach a completion of 1.0 during upload?

I am able to successfully upload files using alamofire. However, I am trying to track the progress of the upload. What I am finding is that, although the upload is successful, as I can see my files successfully uploaded on to the server, the progress tracker never reaches 1.0. It tends to end between 8.00 - (under 1.0) but never reaching 1. This presents problems as I need to handle the completion of the file upload.

 Alamofire.upload(
        multipartFormData: { multipartFormData in
            for(key, value) in sendParamters{
                multipartFormData.append((value.data(using: .utf8)!), withName: key)
            }

            for fileURL in arrayURLToUpload{
                print("fileURL: \(fileURL)")
                multipartFormData.append(fileURL, withName: "file[]")
            }
    },
        to: UPLOAD_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }

                /**TRACK PROGRESS OF UPLOAD**/
                upload.uploadProgress { progress in
                    print(progress.fractionCompleted) // NEVER REACHES 1.0

                    var progress = progress.fractionCompleted


                }
                /***/


            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

console:

 0.041737145652041
 0.521714320650513
 0.772137194562759

Upvotes: 0

Views: 476

Answers (1)

Enea Dume
Enea Dume

Reputation: 3232

You just need to change the postition of

upload.uploadProgress { progress in
                print(progress.fractionCompleted) // NEVER REACHES 1.0

with

  upload.responseJSON { response in
            debugPrint(response)
        }  

This is the correct usage:

Alamofire.upload(
        multipartFormData: { multipartFormData in
            for(key, value) in sendParamters{
                multipartFormData.append((value.data(using: .utf8)!), withName: key)
            }

            for fileURL in arrayURLToUpload{
                print("fileURL: \(fileURL)")
                multipartFormData.append(fileURL, withName: "file[]")
            }
    },
        to: UPLOAD_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

             /**TRACK PROGRESS OF UPLOAD**/
                upload.uploadProgress { progress in
                    print(progress.fractionCompleted)
                }
                /***/

                upload.responseJSON { response in
                    debugPrint(response)
                }

            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

Upvotes: 1

Related Questions