10

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.

Ali Moazenzadeh
  • 584
  • 4
  • 13
  • 1
    I still can't find the answer. In alamofire unit test, there is a method called "executeMultipartFormDataUploadRequestWithProgress" which check if there is any upload progress. Haven't run the test on my own. But it should work with multipartFormData. – Thet Htun Apr 17 '21 at 14:49
  • downloadProgress works though. At the end of successful upload, download progress return value "1". Only uploadProgress closure isn't being called. response callback also works. – Thet Htun Apr 17 '21 at 14:55
  • i have posted "my fix" in the answers. – Thet Htun Apr 21 '21 at 09:23
  • For me, uploadProgress block is only invoked once as if it was completed, but it is invoked at the start of the upload process. Later, response { } callback is invoked when upload has finished. – mixtly87 Jun 02 '22 at 17:14

3 Answers3

5

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.

Thet Htun
  • 471
  • 5
  • 13
2

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)")
         }
Darklegend
  • 61
  • 2
  • 10
  • didn't work for me. May be there's some queue dispatching problem – Ali Moazenzadeh Jun 18 '20 at 20:39
  • @AliMoazenzadeh are you running that call on main thread? are you sure you need to send that `token` in parameters instead of `header` from this line `multipartFormData.append(token.data(using: .utf8)!, withName: "token")` – Darklegend Jun 20 '20 at 05:37
  • Yes, I'm sure. token is for file not for header. and I'm running code on main thread – Ali Moazenzadeh Jun 20 '20 at 07:41
  • have you tried your API on swagger or Postman to check its working? If everything is fine and working on those and backend site do try the code I have added in above post (edited) for you to change for the AF call. @AliMoazenzadeh – Darklegend Jun 20 '20 at 08:29
  • Yes, server is working correctly and responseJSON block is executing every time upload finishes. Does server need any additional header to support progress? I don't think so myself @darklegend – Ali Moazenzadeh Jun 20 '20 at 09:11
  • No server doesn't needs additional headers. If your code is going on the success response then you should be able to go onto `.uploadProgress` portion of the code as well. – Darklegend Jun 20 '20 at 16:38
  • There's no difference between `.uploadProgress { progress in` and `.uploadProgress(closure: { (progress) in` – Eduard Oct 21 '22 at 07:40
0

I also have this issue. I try to solve it by many methods. In the end, I uninstall the pod "CocoaDebug". It worked. So I think maybe is CocoaDebug disturbing the network.

jdy
  • 1
  • 3