过完新年继续学习,让我们来学习下如何计算下载文件大小,并且如何post上传文件
今天才初二,昨天是回家这么长时间以来唯一放开了好好玩儿的一天,玩儿的很开心.开心之后又开始继续研究和复习.首先愿新的一年万事如意,心想事成,最主要身体健康
首先,谈谈如何获取下载资源的信息.
NSString *url_string = @"http://box.dwstatic.com/skin/Teemo/Teemo_Splash_0.jpg";
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_string] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
request.HTTPMethod = @"HEAD";
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", response);
NSLog(@"---------------");
NSLog(@"%@", data);
}
//这个方法只能获得下载资源的信息,不回返回数据体.
除了这个还有其他方式可以获得大小
NSURL *url = [NSURL URLWithString:url_string];//转成url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.f]; //创建成可变网络请求
[request setHTTPMethod:@"GET"];
[NSURLConnection connectionWithRequest:request delegate:self];
实现代理
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"要下载文件大小为 %@",response);
}
至于如何下载下来这个文件,相信大家有很多方法.
至于文件上传.
大家可以先创建一个上传model.
- #import "UploadFile.h"
- @implementation UploadFile
- // 拼接字符串
- static NSString *boundaryStr = @"--"; // 分隔字符串
- static NSString *randomIDStr; // 本次上传标示字符串
- static NSString *uploadID; // 上传(php)脚本中,接收文件字段
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- randomIDStr = @"itcast";
- uploadID = @"uploadFile";
- }
- return self;
- }
- #pragma mark - 私有方法
- - (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile
- {
- NSMutableString *strM = [NSMutableString string];
- [strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];
- [strM appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID, uploadFile];
- [strM appendFormat:@"Content-Type: %@\n\n", mimeType];
- NSLog(@"%@", strM);
- return [strM copy];
- }
- - (NSString *)bottomString
- {
- NSMutableString *strM = [NSMutableString string];
- [strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];
- [strM appendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];
- [strM appendString:@"Submit\n"];
- [strM appendFormat:@"%@%@--\n", boundaryStr, randomIDStr];
- NSLog(@"%@", strM);
- return [strM copy];
- }
- #pragma mark - 上传文件
- - (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data
- {
- // 1> 数据体
- NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"];
- NSString *bottomStr = [self bottomString];
- NSMutableData *dataM = [NSMutableData data];
- [dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];
- [dataM appendData:data];
- [dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];
- // 1. Request
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
- // dataM出了作用域就会被释放,因此不用copy
- request.HTTPBody = dataM;
- // 2> 设置Request的头属性
- request.HTTPMethod = @"POST";
- // 3> 设置Content-Length
- NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];
- [request setValue:strLength forHTTPHeaderField:@"Content-Length"];
- // 4> 设置Content-Type
- NSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];
- [request setValue:strContentType forHTTPHeaderField:@"Content-Type"];
- // 3> 连接服务器发送请求
- [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
- NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@", result);
- }];
- }
- @end
//接着直接在控制器调用- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data 即可.
关于上传的代码来源于http://blog.csdn.net/codywangziham01/article/details/38044637,这篇博客关于文件的下载和上传讲解的都很多很详细.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。