iOS断点续传-上传

iOS断点续传-上传,第1张

断点上传是将文件切桐枣割成小数据片段进行上传,并记录上传进度,当上传完毕后,服务迹顷端将上传的小文件片段进行合并的过程。

采用NSFileHandle对文姿轮陆件数据进行逐段读取上传

要与服务端研发人员约定好上传完毕规则,区分何时上传完毕,进行文件合并。

上传采用NSURLSession/AFN等,逐段上传。

快捷指令post上传文件的步骤为:

1、设置请求行。

2、设置post请求,在iOS开发中对于图片或者文件的罩桐上传,Xcode并没有进行封装,需要自己手动的在代码中拼接请求体。

3、塌闷轿设置连接方式团肆,推荐使用NSURLSession,来替代NSURLConection。

第一种Data Task用于稿含友加载数据,使用全局的shared session和dataTaskWithRequest:completionHandler:方法创建。代码如下:

[objc] view plaincopy

/* 使用NSURLSessionDataTask加载网页键槐数据 */

- (IBAction)loadData:(id)sender {

// 开始加载数据,让spinner转起来

[self.spinner startAnimating]

// 创建Data Task,用于打开我的csdn blog主页

NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net/u010962810"]

NSURLRequest *request = [NSURLRequest requestWithURL:url]

NSURLSession *session = [NSURLSession sharedSession]

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request

completionHandler:

^(NSData *data, NSURLResponse *response, NSError *error) {

// 输出返回的状态码,请求成功的话为200

[self showResponseCode:response]

// 在webView中加载数据

[self.webView loadData:data

MIMEType:@"text/html"

textEncodingName:@"utf-8"

baseURL:nil]

// 加载数据完毕,停止spinner

[self.spinner stopAnimating]

}]

// 使用resume方法启动任务

[dataTask resume]

}

/* 输出http响应的老袭状态码 */

- (void)showResponseCode:(NSURLResponse *)response {

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response

NSInteger responseStatusCode = [httpResponse statusCode]

NSLog(@"%d", responseStatusCode)

}

completionHandler指定任务完成后的动作。注意一定要使用resume方法启动任务。(Upload Task和Download Task同理)

运行结果:

第二种Upload Task用于完成上传文件任务,使用方法类似:

[objc] view plaincopy

/* 使用NSURLSessionUploadTask上传文件 */

- (IBAction)uploadFile:(id)sender {

//NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]

//NSURLRequest *request = [NSURLRequest requestWithURL:URL]

//NSData *data = ...

//

//NSURLSession *session = [NSURLSession sharedSession]

//NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request

// fromData:data

// completionHandler:

// ^(NSData *data, NSURLResponse *response, NSError *error) {

// // ...

// }]

//

//[uploadTask resume]

}

第三种Download Task用于完成下载文件的任务,使用全局的shared session和downloadTaskWithRequest:completionHandler:方法创建。

注意:在下载任务完成后,下载的文件位于tmp目录下,由代码块中的location指定(不妨输出看看),我们必须要在completion handler中将文件放到持久化的目录下保存。代码如下:

[objc] view plaincopy

/* 使用NSURLSessionDownloadTask下载文件 */

- (IBAction)downloadFile:(id)sender {

[self.spinner startAnimating]

NSURL *URL = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/8b13632762d0f703b0faaab00afa513d2697c515.jpg"]

NSURLRequest *request = [NSURLRequest requestWithURL:URL]

NSURLSession *session = [NSURLSession sharedSession]

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request

completionHandler:

^(NSURL *location, NSURLResponse *response, NSError *error) {

[self showResponseCode:response]

// 输出下载文件原来的存放目录

NSLog(@"%@", location)

// 设置文件的存放目标路径

NSString *documentsPath = [self getDocumentsPath]

NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath]

NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]]

// 如果该路径下文件已经存在,就要先将其移除,在移动文件

NSFileManager *fileManager = [NSFileManager defaultManager]

if ([fileManager fileExistsAtPath:[fileURL path] isDirectory:NULL]) {

[fileManager removeItemAtURL:fileURL error:NULL]

}

[fileManager moveItemAtURL:location toURL:fileURL error:NULL]

// 在webView中加载图片文件

NSURLRequest *showImage_request = [NSURLRequest requestWithURL:fileURL]

[self.webView loadRequest:showImage_request]

[self.spinner stopAnimating]

}]

[downloadTask resume]

}

/* 获取Documents文件夹的路径 */

- (NSString *)getDocumentsPath {

NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

NSString *documentsPath = documents[0]

return documentsPath

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12303571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存