我正在尝试通过Alamofire 4.0将视频上传到服务器,我想添加进度条以显示上传过程中上传过程的百分比,我是如何通过Alamofire上传的相同功能来实现的。
我的上传功能代码:
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(url.absoluteURL!, withName: "videoFile", fileName: "alaa", mimeType: "mov")
multipartFormData.append("video".data(using: .utf8)!, withName: "load")
multipartFormData.append("record".data(using: .utf8)!, withName: "type")
}, with: URL, encodingCompletion: { (result) in
// code
print("uploaded")
})
答案 0 :(得分:1)
直接引用AlamoFire文档:
上传进度
当您的用户正在等待他们的上传完成时,有时候 可以方便地显示上传到用户的进度。任何 UploadRequest可以报告上传进度和下载进度 使用uploadProgress和downloadProgress API的响应数据。
let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")
Alamofire.upload(fileURL, to: "https://httpbin.org/post")
.uploadProgress { progress in // main queue by default
print("Upload Progress: \(progress.fractionCompleted)")
}
.downloadProgress { progress in // main queue by default
print("Download Progress: \(progress.fractionCompleted)")
}
.responseJSON { response in
debugPrint(response)
}
答案 1 :(得分:0)
请检查一下。
var imgData = Data()
if image != nil {
imgData = UIImageJPEGRepresentation(image!, 1.0)!
}
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(imgData, withName: "image", fileName: "imagefilename", mimeType: "image/jpeg")
for (key, value) in param {
//let data = (value as! String).data(using: String.Encoding.utf8)!
let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue)
multipartFormData.append(data!, withName: key as! String)
}
},
usingThreshold:UInt64.init(),
to:fullLink,
method:.post,
headers:[:],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress { progress in // main queue by default
print("Upload Progress: \(progress.fractionCompleted)")
}
upload.responseJSON { response in
debugPrint(response)
if let TempresponseDict:NSDictionary = response.result.value as? NSDictionary {
if (TempresponseDict.object(forKey: "response") as? String)?.caseInsensitiveCompare("success") == .orderedSame {
CompletionHandler(true, TempresponseDict)
}
else {
var statusCode = response.response?.statusCode
if let error = response.result.error as? AFError {
statusCode = error._code // statusCode private
switch error {
case .invalidURL(let url):
print("Invalid URL: \(url) - \(error.localizedDescription)")
case .parameterEncodingFailed(let reason):
print("Parameter encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .multipartEncodingFailed(let reason):
print("Multipart encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .responseValidationFailed(let reason):
print("Response validation failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
switch reason {
case .dataFileNil, .dataFileReadFailed:
print("Downloaded file could not be read")
case .missingContentType(let acceptableContentTypes):
print("Content Type Missing: \(acceptableContentTypes)")
case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
case .unacceptableStatusCode(let code):
print("Response status code was unacceptable: \(code)")
statusCode = code
}
case .responseSerializationFailed(let reason):
print("Response serialization failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
// statusCode = 3840 ???? maybe..
}
print("Underlying error: \(error.underlyingError)")
}
else if let error = response.result.error as? URLError {
print("URLError occurred: \(error)")
}
else {
print("Unknown error: \(response.result.error)")
}
print("\(statusCode)") // the status code
CompletionHandler(false, TempresponseDict)
}
}
else {
CompletionHandler(false, NSDictionary())
}
}
case .failure(let encodingError):
print(encodingError)
CompletionHandler(false, NSDictionary())
}
})
}