如何使用Swift检测Alamofire 5的上传进度和上传状态?



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

最新更新