在Alamofire5之前,我们可以用户对uploadRequest的结果进行编码以获得uploadProgress
。
但是现在,在基于Alamofire Documentation上传Alamofire至版本5之后,我们可以使用.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
个问题,但没有一个起作用。
答案 0 :(得分: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)")
}
答案 1 :(得分:0)
如果您碰巧安装了用于网络流量调试的库,例如 Wormholy,那么请查看 this issue thread。简而言之,这是库的问题并卸载它,解决了问题。虽然不确定其他网络调试器。为清楚起见,请尝试在一个新的、干净的项目中进行尝试,看看 alamofire 是否能在这样的环境中工作。
答案 2 :(得分:0)
我也有这个问题。 我尝试通过多种方法解决它。 最后,我卸载了 pod“CocoaDebug”。 有效。 所以我想可能是 CocoaDebug 扰乱了网络。