一.前言
iOS开发中和服务器打交道除了数据请求外,还有文件的上传及下载,OC中文件上传及下载,笔者就不多说了,今天来看下Swift中该如何进行文件的下载及上传,同样笔者做下简单封装,方便在其他地方调用.
二.实现
-1.首先导入Alamofire这个库,并创建数据请求类(继成NSObject)笔者命名为XHNetwork
-2.1.在XHNetwork.swift中import Alamofire
这个库,并创建下列属性:
import UIKit
import Alamofire
class XHNetwork: NSObject {
/**
* 网络请求成功闭包:
*/
typealias XHNetworkSuccess = (response:AnyObject) -> ()
/**
* 网络请求失败闭包:
*/
typealias XHNetworkFailure = (error:NSError) -> ()
/**
* 上传进度闭包:(回调:1.单次上传大小 2.已经上传大小,3.文件总大小)
*/
typealias UploadProgress = (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) -> ()
/**
* 下载进度闭包:(回调:1.单次写入大小 2.已经写入大小,3.文件总大小)
*/
typealias DownloadProgress = (bytesRead:Int64, totalBytesRead:Int64, totalBytesExpectedToRead:Int64) -> ()
/**
* 网络请求单例
*/
static let shareNetwork = XHNetwork()
}
-2.2.文件上传
/**
文件上传
- parameter urlString: URL
- parameter fileURL: 要上传文件路径URL(包含文件名)
- parameter uploadProgress: 上传进度回调(子线程)
- parameter success: 成功回调
- parameter failure: 失败回调
*/
func upload(urlString: String ,fileURL:NSURL,uploadProgress:UploadProgress, success: XHNetworkSuccess, failure: XHNetworkFailure){
Alamofire.upload(.POST,urlString, file: fileURL)
.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
/*
bytesWritten 单次上传大小
totalBytesWritten 已经上传了大小
totalBytesExpectedToWrite 文件总大小
*/
/**
* 子线程回调上传进度
*/
uploadProgress(bytesWritten:bytesWritten,totalBytesWritten:totalBytesWritten,totalBytesExpectedToWrite:totalBytesExpectedToWrite)
}
.validate()
.responseJSON { response in
switch response.result {
case .Success(let value):
/**
* 成功
*/
success(response: value)
case .Failure(let error):
/**
* 失败
*/
failure(error: error)
debugPrint(error)
}
}
}
-2.3.文件下载
/**
文件下载
- parameter urlString: 下载URL
- parameter downloadProgress: 下载进度回调(子线程)
- parameter fileSavePathURL: 文件存储路径URL(不含文件名)
- parameter success: 成功回调
- parameter failure: 失败回调
*/
func download(urlString: String ,savePathURL:NSURL ,downloadProgress: DownloadProgress, success: XHNetworkSuccess, failure: XHNetworkFailure){
Alamofire.download(.GET, urlString) { temporaryURL, response in
let pathComponent = response.suggestedFilename//建议文件名
return savePathURL.URLByAppendingPathComponent(pathComponent!)
}.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
/*
bytesRead 单次下载大小
totalBytesRead 已经下载大小
totalBytesExpectedToRead 文件总大小
*/
/**
* 子线程回调下载进度
*/
downloadProgress(bytesRead:bytesRead,totalBytesRead:totalBytesRead,totalBytesExpectedToRead:totalBytesExpectedToRead)
}
.response { _, _, _, error in
if let error = error {
/**
* 失败
*/
failure(error:error)
debugPrint("Failed with error: \(error)")
} else {
/**
* 成功
*/
success(response: savePathURL.absoluteString)
}
}
}
三.调用
-1.1.文件上传-调用
//MARK : - 文件上传
XHNetwork.shareNetwork.upload(上传URLString , fileURL: 文件完整路径URL, uploadProgress: { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
/**
* 子线程回调上传进度
*/
debugPrint("单次上传大小:\(bytesWritten)___一共上传大小:\(totalBytesWritten)___总大小:\(totalBytesExpectedToWrite)")
/**
* 如需进行UI处理,请到主线程操作
*/
dispatch_async(dispatch_get_main_queue()) {
//处理UI
}
}, success: { (response) in
/*
成功
*/
debugPrint(response)
}) { (error) in
/*
失败
*/
debugPrint(error)
}
-1.2.文件下载-调用
//MARK: - 下载
//文件保存路径
let savePathURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
XHNetwork.shareNetwork.download("下载URLString", savePathURL: savePathURL
, downloadProgress: { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
/**
* 子线程回调下载进度
*/
debugPrint("单次下载大小:\(bytesRead)___一共下载大小:\(totalBytesRead)___总大小:\(totalBytesExpectedToRead)")
/**
* 如需进行UI处理,请到主线程操作
*/
dispatch_async(dispatch_get_main_queue()) {
//处理UI
}
}, success: { (response) in
/*
成功(回调文件存储路劲)
*/
debugPrint(response)
}) { (error) in
/*
失败
*/
debugPrint(error)
}
四.小节
- 网络编程常用功能就介绍到这里,还有需求的同学,可以去看下Alamofire官方文档
- 传送门:Swift网络编程(一)数据请求
- 代码地址:https://github.com/CoderZhuXH/XHNetworkSwift
网友评论