简体   繁体   English

获取分段上传 Alamofire5 的上传进度

[英]Get upload progress for multipart upload Alamofire5

Before Alamofire5 we could user encodingReesult of uploadRequest to get uploadProgress .在 Alamofire5 之前,我们可以使用 uploadRequest 的 encodingReesult 来获取uploadProgress But now after uploading Alamofire to version 5, based on Alamofire Documentation , we can use .uploadProgress in order to get upload progress handler.但是现在在将 Alamofire 上传到版本 5 之后,基于Alamofire 文档,我们可以使用.uploadProgress来获取上传进度处理程序。

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.但是在上传过程中从不调用uploadProgress闭包。

I've checked many stackoverflow questions but no one worked.我检查了许多stackoverflow问题,但没有一个有效。

replace your更换你的

.uploadProgress { progress in 
            print(progress)
        }

with

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

it will give you output as:它会给你 output 为:

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)")
         }

If u happen to have installed a library for network traffic debugging like Wormholy , then, checkout this issue thread .如果你碰巧安装了一个用于网络流量调试的库,比如Wormholy ,那么,请查看这个问题线程 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.为了清楚起见,尝试在一个新的、干净的项目中玩耍,看看 alamofire 是否在这样的环境中工作。

I also have this issue.我也有这个问题。 I try to solve it by many methods.我尝试通过多种方法解决它。 In the end, I uninstall the pod "CocoaDebug".最后,我卸载了 pod “CocoaDebug”。 It worked.有效。 So I think maybe is CocoaDebug disturbing the network.所以我认为可能是 CocoaDebug 扰乱了网络。

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

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