6

在 Alamofire5 之前,我们可以使用 uploadRequest 的 encodingReesult 来获取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)
        }

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

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

4

3 回答 3

2

如果你碰巧安装了一个用于网络流量调试的库,比如Wormholy,那么,请查看这个问题线程。简而言之,这是库和卸载它的问题,解决了问题。虽然不确定其他网络调试器。为了清楚起见,尝试在一个新的、干净的项目中玩耍,看看 alamofire 是否在这样的环境中工作。

于 2021-04-21T09:22:38.930 回答
1

更换你的

.uploadProgress { progress in 
            print(progress)
        }

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

它会给你输出:

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

编辑 :

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)")
         }
于 2020-06-18T18:45:59.527 回答
0

我也有这个问题。我尝试通过多种方法解决它。最后,我卸载了 pod “CocoaDebug”。有效。所以我认为可能是 CocoaDebug 扰乱了网络。

于 2021-04-26T03:20:14.313 回答