如何使用 Swift 在 Alamofire 5 中检测上传进度和上传状态?
How to detect upload progress and upload status in Alamofire 5 with Swift?
在alamorefire的文档中,检测上传进度只有这个例子
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
}, to: "https://httpbin.org/post").responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
但我的期望是我想在完成后检测它的上传进度和上传状态
switch result {
case .success():
//Success code
break
case .failure:
//show error upload failed
break
}
试试这个:-
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)")
}
}
在alamorefire的文档中,检测上传进度只有这个例子
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
}, to: "https://httpbin.org/post").responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
但我的期望是我想在完成后检测它的上传进度和上传状态
switch result {
case .success():
//Success code
break
case .failure:
//show error upload failed
break
}
试试这个:-
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)")
}
}