import
UIKit
class
ViewController
:
UIViewController
,
NSURLSessionDownloadDelegate
{
override
viewDidLoad() {
super
.viewDidLoad()
sessionSeniorDownload()
}
//下载文件
sessionSeniorDownload(){
//下载地址
url =
(string:
"http://hangge.com/blog/images/logo.png"
)
//请求
: url!)
session = currentSession()
NSURLSession
//下载任务
downloadTask = session.downloadTaskWithRequest(request)
//使用resume方法启动任务
downloadTask.resume()
}
//创建一个下载模式
currentSession() ->
{
var
predicate:dispatch_once_t = 0
currentSession:
? =
nil
dispatch_once(&predicate,{
config =
NSURLSessionConfiguration
.defaultSessionConfiguration()
currentSession =
(configuration: config,delegate:
self
delegateQueue:
)
})
return
currentSession!
}
//下载代理方法,下载结束
func
URLSession
(session:
NSURLSessionDownloadTask
didFinishDownloadingToURL location:
) {
//下载结束
(
"下载结束"
)
//输出下载文件原来的存放目录
"location:(location)"
)
//location位置转换
locationPath = location.path
//拷贝到用户目录
documnets:
NSHomeDirectory
() +
"/Documents/2.png"
//创建文件管理器
fileManager:
NSFileManager
.defaultManager()
"new location:(documnets)"
)
}
//下载代理方法,监听下载进度
didWriteData bytesWritten:
Int64
totalBytesExpectedToWrite:
) {
//获取进度
written:
CGFloat
= (
)(totalBytesWritten)
total:
)(totalBytesExpectedToWrite)
pro:
= written/total
"下载进度:(pro)"
)
}
//下载代理方法,下载偏移
didResumeAtOffset fileOffset:
) {
//下载偏移,主要用于暂停续传
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
3,使用Upload Task来上传文件
22
sessionUpload(){
//上传地址
"http://hangge.com/"
)
//请求
: url!)
.sharedSession()
//上传数据流
documents =
"/Documents/1.png"
imgData =
NSData
(contentsOfFile: documents)
uploadTask = session.uploadTaskWithRequest(request,fromData: imgData) {
(data:
NSURLResponse
NSError
?) ->
in
//上传完毕后
"上传完毕"
)
//使用resume方法启动任务
uploadTask.resume()
}
|
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|