使用Alamofire为文件上传添加进度

编程入门 行业动态 更新时间:2024-10-25 08:27:48
本文介绍了使用Alamofire为文件上传添加进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获取文件上传进度?

How can I get progress of file uploading?

// import Alamofire func uploadWithAlamofire() { let image = UIImage(named: "myImage")! // define parameters let parameters = [ "hometown": "yalikavak", "living": "istanbul" ] // Begin upload Alamofire.upload(.POST, "upload_url", // define your headers here headers: ["Authorization": "auth_token"], multipartFormData: { multipartFormData in // import image to request if let imageData = UIImageJPEGRepresentation(image, 1) { multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png") } // import parameters for (key, value) in parameters { multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) } }, // you can customise Threshold if you wish. This is the alamofire's default value encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { response in debugPrint(response) } case .Failure(let encodingError): print(encodingError) } }) }

我知道类似的东西必须添加进度条,但是不知道在哪里可以添加进度条。

I know something like I have to add progress block but don't know where can I add that progress block.

.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)") }

我想在上面添加块,以及如何计算估计的视频上传时间。

I want to add above block and how can I calculate estimated time for video uploading.

推荐答案

您可以这样做:

Alamofire.upload( .POST, URLString: "httpbin/post", multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn") multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow") }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in let progress: Float = Float(totalBytesRead)/Float(totalBytesExpectedToRead) // you can give this progress to progressbar progress let value = Int(progress * 100) // this is the percentage of the video uploading print(totalBytesRead) } upload.responseJSON { request, response, result in debugPrint(result) } case .Failure(let encodingError): print(encodingError) } } )

更多推荐

使用Alamofire为文件上传添加进度

本文发布于:2023-11-26 03:30:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   文件上传   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!