欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

【IOS学习】http异步文件上传和下载及进度指示

程序员文章站 2023-03-28 10:16:11
文件下载和进度 nodejs服务端下载图片 先改造一下我们的服务端程序,来下载一张图片,代码如下 //下载返回文件流 function download(...

文件下载和进度

nodejs服务端下载图片

先改造一下我们的服务端程序,来下载一张图片,代码如下


    //下载返回文件流
	function download(req,res){
		//写入头
	    var downloadfilepath = "./1.jpg";
	    var filename = path.basename(downloadfilepath);
	    var filesize = fs.readfilesync(downloadfilepath).length;
	    res.setheader('content-disposition','attachment;filename=' + filename);//此处是关键
	    res.setheader('content-length',filesize);
	    res.setheader('content-type','application/octet-stream');
	    var filestream = fs.createreadstream(downloadfilepath,{buffersize:1024 * 1024});
		 filestream.pipe(res,{end:true});
		// res.writehead(200, {'content-type': 'text/html'});
	}

	//改造一下handler方法,让url访问/download的时候进入文件下载的方法,返回文件流
	handler:function(req,res){
        console.log('handler');
        console.log(req.url);
        switch(req.url){
            case '/' : get(req,res); break;
            case "/download" : download(req,res); break;
        }

    }

ios请求下载文件流

//http下载文件流
- (void)download{
    //string 转 url编码
    nsstring *urlstring = @"https://localhost:8001/download";
    nsurl *url = [nsurl urlwithstring:[urlstring stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset urlqueryallowedcharacterset]]];
    nsurlrequest *request = [[nsurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];
    nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self];
    [connection start];
}


修改下请求委托,打印出请求头和收到的data,从而看一下收到的数据大小和数据请求进度相关内容。

//接收响应
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response{
    nslog(@"=================didreceiveresponse=================");
    nshttpurlresponse *resp = (nshttpurlresponse *)response;
    nslog(@"response:%@",resp);

}

//接收响应
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data{
    nslog(@"=================didreceivedata=================");
    nslog(@"data.length:%lu",(unsigned long)data.length);

}

完成请求后打印出的结果如下:

2016-02-11 21:48:50.694 network-demo[14461:1033375] =================request redirectresponse=================
2016-02-11 21:48:50.696 network-demo[14461:1033375] request: { url: https://localhost:8001/download }
2016-02-11 21:48:50.706 network-demo[14461:1033375] =================didreceiveresponse=================
2016-02-11 21:48:50.706 network-demo[14461:1033375] response: { url: https://localhost:8001/download } { status code: 200, headers {
    connection = "keep-alive";
    "content-disposition" = "attachment;filename=1.jpg";
    "content-length" = 19557;
    "content-type" = "application/octet-stream";
    date = "thu, 11 feb 2016 13:48:50 gmt";
} }
2016-02-11 21:48:50.706 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 21:48:56.148 network-demo[14461:1033375] data.length:19557
2016-02-11 21:48:56.148 network-demo[14461:1033375] =================connectiondidfinishloading=================

可以看出:服务端在resqonse头中加入了content-length信息,告知整个流的大小,不过因为图片文件本身 较小,所以并没有分包,因此didreceivedata方法只调用了一次就完成了文件传递。大家可以试着修改下服务端返回的文件, 改为一个较大点的文件来试一次,这里我改成一个稍微大一些的图片,一张我自己hhkb键盘的美图~

var downloadfilepath = "./img_0222.jpg";再试着发一次请求:

2016-02-11 22:09:14.088 network-demo[14461:1033375] =================request redirectresponse=================
2016-02-11 22:09:14.089 network-demo[14461:1033375] request: { url: https://localhost:8001/download }
2016-02-11 22:09:14.118 network-demo[14461:1033375] =================didreceiveresponse=================
2016-02-11 22:09:14.119 network-demo[14461:1033375] response: { url: https://localhost:8001/download } { status code: 200, headers {
    connection = "keep-alive";
    "content-disposition" = "attachment;filename=img_0222.jpg";
    "content-length" = 1265302;
    "content-type" = "application/octet-stream";
    date = "thu, 11 feb 2016 14:09:14 gmt";
} }
2016-02-11 22:09:14.119 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.119 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.120 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.120 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.121 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.121 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.122 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.122 network-demo[14461:1033375] data.length:131072
2016-02-11 22:09:14.123 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.123 network-demo[14461:1033375] data.length:132000
2016-02-11 22:09:14.123 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.124 network-demo[14461:1033375] data.length:392288
2016-02-11 22:09:14.124 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.124 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.124 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.125 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.125 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.125 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.125 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.125 network-demo[14461:1033375] data.length:65536
2016-02-11 22:09:14.126 network-demo[14461:1033375] =================didreceivedata=================
2016-02-11 22:09:14.126 network-demo[14461:1033375] data.length:151190
2016-02-11 22:09:14.126 network-demo[14461:1033375] =================connectiondidfinishloading=================

可以看到didreceivedata委托被反复调用了很多次,我们可以通过data.length:151190和"content-length" = 1265302;就可以计算出流的下载进度。

  //获取content-length
  //[[((nshttpurlresponse *)response) allheaderfields]objectforkey:@"content-length"]

获取到完成的data后,可以直接把二进制的data转换成图片,代码如下:

   uiimage *img = [uiimage imagewithdata:data];
   uiimageview *imageview = [[uiimageview alloc]initwithimage:img];
   [imageview setframe:cgrectmake(30, 30, 200, 200)];
   [self.view addsubview:imageview];

上传文件和进度

服务端代码

服务端使用nodejs写的接受图片上传,重命名并保存文件,使用了formidable这个库完成图片获取,作为demo写的比较简单大家随意感受下。


//文件上传
	function upload(req,res){
		//创建上传表单
		var form = new formidable.incomingform();
		//设置编辑
		form.encoding = 'utf-8';
		//设置上传目录
		form.uploaddir = './upload/';
		form.keepextensions = true;
		//文件大小
		form.maxfieldssize = 10 * 1024 * 1024;
		form.parse(req, function (err, fields, files) {
			if(err) {
				res.send(err);
				return;
			}
			// console.log(fields);
			console.log("=====");
			// console.log(files);
			// console.log(files.file.name);
			var extname = /\.[^\.]+/.exec(files.file.name);
			var ext = array.isarray(extname)
				? extname[0]
				: '';
			//重命名,以防文件重复
			var avatarname = uuid() + ext;
			//移动的文件目录
			var newpath = form.uploaddir + avatarname;
			fs.renamesync(files.file.path, newpath);
			// res.send('success');
			var msg = { "status":1,"msg":"succeed"}
			res.write(json.stringify(msg));
			res.end();
		});
	}

http文件传输协议

来说点文件上传http协议的基础,前面的demo中,我们都没有设置请求头,因为我们都使用了默认的请求头content-type:application/x-www-form-urlencoded,这个请求头就是和html中的表单上传,如果get请求则 数据在url中,如果post请求,数据默认放在请求体中。然后默认的x-www-form-urlencoded头并不能上传文件,上传文件需要 设置头为:content-type:multipart/form-data; boundary=yy,boundary用于标识边界,可以自定义,使用时前面需要加上两个–,例如:”–yy”

我们在ios上传文件时需要这样设置请求头

  /** 设置请求头 */
    // 请求体的长度
    [request setvalue:[nsstring stringwithformat:@"%zd", body.length] forhttpheaderfield:@"content-length"];
    // 声明这个post请求是个文件上传
    [request setvalue:@"multipart/form-data; boundary=yy" forhttpheaderfield:@"content-type"];
    [request sethttpmethod:@"post"];

ios文件上传代码

我们上传一张稍微大点的图片,直接使用nsbundle对象读取项目中的文件,然后设置请求相关的委托方法,代码如下



//http上传文件流
- (void)upload{

    #define encode(str) [str datausingencoding:nsutf8stringencoding]

    nsurl *dataurl = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"img_0222" oftype:@"jpg"]];
    nsdata *data = [nsdata datawithcontentsofurl:dataurl];

    //string 转 url编码
    nsstring *urlstring = @"https://localhost:8001/upload";
    nsurl *url = [nsurl urlwithstring:[urlstring stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset urlqueryallowedcharacterset]]];

    nsmutableurlrequest *request = [[nsmutableurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];

    /** 设置请求头 */
    nsmutabledata *body = [nsmutabledata data];

    //文件参数
    // 参数开始的标志
    [body appenddata:encode(@"--yy\r\n")];
    // name : 指定参数名(必须跟服务器端保持一致)
    // filename : 文件名
    nsstring *disposition = [nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"file", @"1.jpg"];
    [body appenddata:encode(disposition)];
    nsstring *type = [nsstring stringwithformat:@"content-type: %@\r\n", @"multipart/form-data"];
    [body appenddata:encode(type)];
    [body appenddata:encode(@"\r\n")];

    //添加图片数据
    [body appenddata:data];
    [body appenddata:encode(@"\r\n")];
    //结束符
    [body appenddata:encode(@"--yy--\r\n")];
    //把body添加到request中
    [request sethttpbody:body];

    /** 设置请求头 */
    // 请求体的长度
    [request setvalue:[nsstring stringwithformat:@"%zd", body.length] forhttpheaderfield:@"content-length"];
    // 声明这个post请求是个文件上传
    [request setvalue:@"multipart/form-data; boundary=yy" forhttpheaderfield:@"content-type"];
    [request sethttpmethod:@"post"];


    nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self];
    [connection start];
}


#pragma mark -网络请求委托

//请求失败
- (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error{
    nslog(@"=================didfailwitherror=================");
    nslog(@"error:%@",error);
}

//重定向
- (nullable nsurlrequest *)connection:(nsurlconnection *)connection willsendrequest:(nsurlrequest *)request redirectresponse:(nullable nsurlresponse *)response{
    nslog(@"=================request redirectresponse=================");
    nslog(@"request:%@",request);
    return request;
}

//接收响应
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response{
    nslog(@"=================didreceiveresponse=================");
    nshttpurlresponse *resp = (nshttpurlresponse *)response;
    nslog(@"response:%@",resp);

}

//接收响应
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data{
    nslog(@"=================didreceivedata=================");
    //    uiimage *img = [uiimage imagewithdata:data];
    //    uiimageview *imageview = [[uiimageview alloc]initwithimage:img];
    //    [imageview setframe:cgrectmake(30, 30, 200, 200)];
    //    [self.view addsubview:imageview];

    nslog(@"data.length:%lu",(unsigned long)data.length);
    if (data) {
        nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil];
        nslog(@"data:%@",dic);
    }
}

//上传数据委托,用于显示上传进度
- (void)connection:(nsurlconnection *)connection   didsendbodydata:(nsinteger)byteswritten
 totalbyteswritten:(nsinteger)totalbyteswritten
totalbytesexpectedtowrite:(nsinteger)totalbytesexpectedtowrite{
    nslog(@"=================totalbyteswritten=================");
    nslog(@"didsendbodydata:%ld,totalbyteswritten:%ld,totalbytesexpectedtowrite:%ld",(long)byteswritten,(long)totalbyteswritten,(long)totalbytesexpectedtowrite);
    nslog(@"上传进度%ld%%",(long)(totalbyteswritten*100 / totalbytesexpectedtowrite));
}

//完成请求
- (void)connectiondidfinishloading:(nsurlconnection *)connection{
    nslog(@"=================connectiondidfinishloading=================");
}

大家可以看下代码,重点可以看下upload方法,和- (void)connection:(nsurlconnection *)connection didsendbodydata:(nsinteger)byteswritten totalbyteswritten:(nsinteger)totalbyteswritten这个委托,观察如何设置文件上传的请求头和请求体,如果获取上传文件的进度。

程序运行后打印的结果

服务端日志,打印请求头

{ host: 'localhost:8001',
  'content-type': 'multipart/form-data; boundary=yy',
  connection: 'keep-alive',
  accept: '*/*',
  'user-agent': 'network-demo/1 cfnetwork/758.0.2 darwin/14.5.0',
  'content-length': '1265418',
  'accept-language': 'en-us',
  'accept-encoding': 'gzip, deflate' }

客户端日志:

2016-02-12 13:05:07.330 network-demo[16708:1254465] =================request redirectresponse=================
2016-02-12 13:05:07.331 network-demo[16708:1254465] request: { url: https://localhost:8001/upload }
2016-02-12 13:05:07.339 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.339 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:32768,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.339 network-demo[16708:1254465] 上传进度2%
2016-02-12 13:05:07.339 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.339 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:65536,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.339 network-demo[16708:1254465] 上传进度5%
2016-02-12 13:05:07.340 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.340 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:98304,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.340 network-demo[16708:1254465] 上传进度7%
2016-02-12 13:05:07.340 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.340 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:131072,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.340 network-demo[16708:1254465] 上传进度10%
2016-02-12 13:05:07.340 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.341 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:163840,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.341 network-demo[16708:1254465] 上传进度12%
2016-02-12 13:05:07.341 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.341 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:196608,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.341 network-demo[16708:1254465] 上传进度15%
2016-02-12 13:05:07.341 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.341 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:229376,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.342 network-demo[16708:1254465] 上传进度18%
2016-02-12 13:05:07.342 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.342 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:262144,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.342 network-demo[16708:1254465] 上传进度20%
2016-02-12 13:05:07.342 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.342 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:294912,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.342 network-demo[16708:1254465] 上传进度23%
2016-02-12 13:05:07.343 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.343 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:327680,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.343 network-demo[16708:1254465] 上传进度25%
2016-02-12 13:05:07.343 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.343 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:360448,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.343 network-demo[16708:1254465] 上传进度28%
2016-02-12 13:05:07.343 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.343 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:393216,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.344 network-demo[16708:1254465] 上传进度31%
2016-02-12 13:05:07.344 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.344 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:425984,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.344 network-demo[16708:1254465] 上传进度33%
2016-02-12 13:05:07.354 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.354 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:458752,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.354 network-demo[16708:1254465] 上传进度36%
2016-02-12 13:05:07.354 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.354 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:491520,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.354 network-demo[16708:1254465] 上传进度38%
2016-02-12 13:05:07.354 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.354 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:524288,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.355 network-demo[16708:1254465] 上传进度41%
2016-02-12 13:05:07.355 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.355 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:557056,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.355 network-demo[16708:1254465] 上传进度44%
2016-02-12 13:05:07.355 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.355 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:589824,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.355 network-demo[16708:1254465] 上传进度46%
2016-02-12 13:05:07.356 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.356 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:622592,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.356 network-demo[16708:1254465] 上传进度49%
2016-02-12 13:05:07.356 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.356 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:655360,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.356 network-demo[16708:1254465] 上传进度51%
2016-02-12 13:05:07.356 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.357 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:688128,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.357 network-demo[16708:1254465] 上传进度54%
2016-02-12 13:05:07.357 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.357 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:720896,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.357 network-demo[16708:1254465] 上传进度56%
2016-02-12 13:05:07.357 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.357 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:753664,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.357 network-demo[16708:1254465] 上传进度59%
2016-02-12 13:05:07.358 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.358 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:786432,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.358 network-demo[16708:1254465] 上传进度62%
2016-02-12 13:05:07.358 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.358 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:819200,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.359 network-demo[16708:1254465] 上传进度64%
2016-02-12 13:05:07.359 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.359 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:851968,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.359 network-demo[16708:1254465] 上传进度67%
2016-02-12 13:05:07.359 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.359 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:884736,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.359 network-demo[16708:1254465] 上传进度69%
2016-02-12 13:05:07.359 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.360 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:917504,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.360 network-demo[16708:1254465] 上传进度72%
2016-02-12 13:05:07.360 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.360 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:950272,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.360 network-demo[16708:1254465] 上传进度75%
2016-02-12 13:05:07.360 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.360 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:983040,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.361 network-demo[16708:1254465] 上传进度77%
2016-02-12 13:05:07.374 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.375 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1015808,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.375 network-demo[16708:1254465] 上传进度80%
2016-02-12 13:05:07.375 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.375 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1048576,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.375 network-demo[16708:1254465] 上传进度82%
2016-02-12 13:05:07.375 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.375 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1081344,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.375 network-demo[16708:1254465] 上传进度85%
2016-02-12 13:05:07.375 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.376 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1114112,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.376 network-demo[16708:1254465] 上传进度88%
2016-02-12 13:05:07.376 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.376 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1146880,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.376 network-demo[16708:1254465] 上传进度90%
2016-02-12 13:05:07.376 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.376 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1179648,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.376 network-demo[16708:1254465] 上传进度93%
2016-02-12 13:05:07.377 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.377 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1212416,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.377 network-demo[16708:1254465] 上传进度95%
2016-02-12 13:05:07.377 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.377 network-demo[16708:1254465] didsendbodydata:32768,totalbyteswritten:1245184,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.377 network-demo[16708:1254465] 上传进度98%
2016-02-12 13:05:07.377 network-demo[16708:1254465] =================totalbyteswritten=================
2016-02-12 13:05:07.377 network-demo[16708:1254465] didsendbodydata:20234,totalbyteswritten:1265418,totalbytesexpectedtowrite:1265418
2016-02-12 13:05:07.378 network-demo[16708:1254465] 上传进度100%
2016-02-12 13:05:07.404 network-demo[16708:1254465] =================didreceiveresponse=================
2016-02-12 13:05:07.405 network-demo[16708:1254465] response: { url: https://localhost:8001/upload } { status code: 200, headers {
    connection = "keep-alive";
    date = "fri, 12 feb 2016 05:05:07 gmt";
    "transfer-encoding" = identity;
} }
2016-02-12 13:05:07.405 network-demo[16708:1254465] =================didreceivedata=================
2016-02-12 13:05:07.405 network-demo[16708:1254465] data.length:28
2016-02-12 13:05:07.405 network-demo[16708:1254465] data:{
    msg = succeed;
    status = 1;
}
2016-02-12 13:05:07.405 network-demo[16708:1254465] =================connectiondidfinishloading=================

大家可以看出如何读取文件上传的进度了。

总结异步http请求

使用异步http请求代码量复杂,但是有许多其他方式达不到的优点

  • 使用文件流上传和下载,节省内存
  • 文件上传和下载有进度提示
  • 可以处理url验证
  • 可以取消在请求过程中取消请求( 使用[connection cancel]方法)

例如在demo中注释的一段代码:

- (void)connection:(nsurlconnection *)connection   didsendbodydata:(nsinteger)byteswritten
 totalbyteswritten:(nsinteger)totalbyteswritten
totalbytesexpectedtowrite:(nsinteger)totalbytesexpectedtowrite{
    ...
    ...

    //测试取消上传
    //if((totalbyteswritten*100 / totalbytesexpectedtowrite) > 50){[connection cancel];}
 }

测试当上传进度到50%的时候,取消文件上传。

请用大一点的图片进行测试,因为这段代码是有bug的,当文件太小不会进入这个委托方法。