Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire 4 multipart request upload progress

How should I track progress of my multipart upload request using Alamofire 4?

My encodingCompletion handler:

encodingCompletion: {
        encodingResult in
        switch encodingResult {
        case .success(let uploadRequest, _, _):
            uploadRequest.uploadProgress {
                p in
                print(p.completedUnitCount, p.totalUnitCount)
            }
            break
        case .failure( _):
            print("Failed to encode upload")
        }
}

The error I get says:

Cannot call value of not-function type 'Progress'

like image 357
Nejc Avatar asked Oct 07 '16 11:10

Nejc


2 Answers

Try this:

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(URL(string: "http://example.com/url1")!, withName: "one")
            multipartFormData.append(URL(string: "http://example.com/url2")!, withName: "two")
        },
        to: "http://example.com/to",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
                upload.uploadProgress { progress in

                    print(progress.fractionCompleted)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )
like image 141
pedrouan Avatar answered Oct 13 '22 20:10

pedrouan


You need to wrap the fractionCompleted, totalUnitCount and completedUnitCount with a cast to Int or Float (depending on what you need).

It works!

source: https://github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449

like image 45
simalexan Avatar answered Oct 13 '22 21:10

simalexan