Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get upload progress for multipart upload Alamofire5

Before Alamofire5 we could user encodingReesult of uploadRequest to get uploadProgress. But now after uploading Alamofire to version 5, based on Alamofire Documentation, we can use .uploadProgress in order to get upload progress handler.

Here's my code:

AF.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(fileContent, withName: "file", fileName: filePath.lastPathComponent)
            multipartFormData.append(token.data(using: .utf8)!, withName: "token")
        }, to: uploadURL)
        .uploadProgress { progress in 
            print(progress)
        }
        .responseJSON { [weak self] response in
            print(response)
        }

But uploadProgress closure never called during upload progress.

I've checked many stackoverflow questions but no one worked.

like image 918
Ali Moazenzadeh Avatar asked Jun 15 '20 08:06

Ali Moazenzadeh


2 Answers

If u happen to have installed a library for network traffic debugging like Wormholy, then, checkout this issue thread. In short note, it's the issue with the library & uninstalling it, solves the problem. Not sure with other network debuggers though. To make things clear, try to playaround in a new, clean project and see if alamofire works in such environment.

like image 180
Thet Htun Avatar answered Oct 25 '22 17:10

Thet Htun


replace your

.uploadProgress { progress in 
            print(progress)
        }

with

.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

it will give you output as :

Upload Progress: 0.035203331252732804
Upload Progress: 0.035203331252732804
Upload Progress: 0.0528049968790992
Upload Progress: 0.088008328131832
Upload Progress: 0.1584149906372976
Upload Progress: 0.2112199875163968
Upload Progress: 0.2288216531427632
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962
Upload Progress: 0.24642331876912962

Edit :

AF.upload(multipartFormData: { MultipartFormData in
        MultipartFormData.append(fileContent, withName: "file" , fileName: filePath.lastPathComponent , mimeType: "image/jpeg")
        for(key,value) in dictonary {
            MultipartFormData.append(token.data(using: String.Encoding.utf8)!, withName: "token")
        }
    }, to: uploadURL, method: .post, headers: ["Content-Type": "application/json")

        .uploadProgress(closure: { (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
        })

        .responseJSON{ (response) in
            debugPrint("SUCCESS RESPONSE: \(response)")
         }
like image 26
Darklegend Avatar answered Oct 25 '22 19:10

Darklegend