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

Ali*_*deh 6 swift alamofire alamofire5

在 Alamofire5 之前,我们可以使用 uploadRequest 的 encodingReresult 来获取uploadProgress. 但是现在在将 Alamofire 上传到版本 5 之后,基于Alamofire 文档,我们可以使用.uploadProgress来获取上传进度处理程序。

这是我的代码:

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)
        }
Run Code Online (Sandbox Code Playgroud)

但是uploadProgress在上传过程中从未调用过关闭。

我检查了很多stackoverflow问题,但没有一个工作。

The*_*tun 6

如果您碰巧安装了像Wormholy这样的网络流量调试库,那么请查看此问题线程。简而言之,这是库的问题,卸载它即可解决问题。但不确定其他网络调试器。为了让事情变得清楚,尝试在一个新的、干净的项目中进行测试,看看 alamofire 是否可以在这样的环境中工作。


Dar*_*end 4

替换你的

.uploadProgress { progress in 
            print(progress)
        }
Run Code Online (Sandbox Code Playgroud)

.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
Run Code Online (Sandbox Code Playgroud)

它会给你输出:

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
Run Code Online (Sandbox Code Playgroud)

编辑 :

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)")
         }
Run Code Online (Sandbox Code Playgroud)