IOS如何通过HTTP公众号如何上传音频频

iOS通过http post上传图片 - 简书
iOS通过http post上传图片
由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他语言那样通过html表单的post就能上传。上传图片的http post请求的格式是这样的:
Content-type: multipart/form-data, boundary=AaB03x
content-disposition: form- name="field1"
Hello Boris!
content-disposition: form- name="pic"; filename="boris.png"
Content-Type: image/png
... contents of boris.png ...
--AaB03x--
第一行是指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。boundary=AaB03x说明了AaB03x为分界线。比如 --AaB03x 就是一个分界线的意思
content-disposition: form- name="field1"
Hello Boris!
这句话声明了请求中的一个字段的名称,如field1
以及字段的值,如Hello Boris!这里类似form表单中的&input name="field1" type="text" value="Hello Boris!"/&中间的空行是必须的。
不同的字段之间用分界线分开,分界线需要单独一行,如 --AaB03x--
分界线的下一行,是下一个字段
content-disposition: form- name="pic"; filename="boris.png"
Content-Type: image/png
... contents of boris.png ...
--AaB03x--
这里声明了变量pic,也就是我们要传的文件,上传文件的时候需要在后边指定file name:filename="boris.png"并且需要在下一行指定文件的格式:Content-Type: image/png
... contents of boris.png ...
这里是boris.png的二进制内容,如 &d0a1a0a 000b4
b2af91 2c dd79b724 6bc888c88 8c9c8733 55ddb1d5 6a0db486
在http post请求的结尾,需要有一个分界线,但是是前后都有--的:--AaB03x--
以上的这些格式,是http的规范,每个空行,空格都是必须的。
下边是iOS的实现代码:
//分界线的标识符
NSString TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//根据url初始化request
NSMutableURLRequest request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
//要上传的图片
UIImage image=[params objectForKey:@"pic"];
//得到图片的data
NSData data = UIImagePNGRepresentation(image);
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [params allKeys];
//遍历keys
for(int i=0;i&[keys count];i++)
//得到当前key
NSString *key=[keys objectAtIndex:i];
//如果key不是pic,说明value是字符类型,比如name:Boris
if(![key isEqualToString:@"pic"])
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form- name="%@"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[params objectForKey:key]];
////添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@"Content-Disposition: form- name="pic"; filename="boris.png"\r\n"];
//声明上传文件的格式
[body appendFormat:@"Content-Type: image/png\r\n\r\n"];
//声明结束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//将image的data加入
[myRequestData appendData:data];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form- boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
//建立连接,设置代理
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//设置接受response的data
if (conn) {
mResponseData = [[NSMutableData data] retain];
由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他语言那样通过html表单的post就能上传。 上传图片的http post请求的格式是这样的: Content-type: multipart/form-data, boundar...
(NSMutableURLRequest *)PostImageRequest:(NSString )URLStringUIImage:(UIImage)imageparameters:(NSDictionary *)parameterssuccess:(void (^)(...
+(NSString *)PostImagesToServer:(NSString *) strUrl dicPostParams:(NSMutableDictionary *)params dicImages:(NSMutableDictionary *) dicImag...
下面的截图,就是Img以file文件类型上传: 1.AFNetworking最新,3.0中也有封装好的方法,不过暂时没弄出来,项目做完在总结。 2.就是用系统自带就行处理. 下面是封装好的,绝对可用,只需要改变参数就可以了 下面加粗,斜线的地方,就是你参数名,需要修改的地方...
由于公司最近项目中 有个需求是要把你扫描获得的图片上传到ftp服务器 ,建于以前做的基本都是通过http 上传至服务器加上以前没有做过上传至ftp服务器,所以在此总结一下网上百度到的基本处理方法。
先说一下基本简介,在iOS端的ftp上传使用的是CFNetWork框...
我渺小地了解这世界的庞大 他曾不动声色地欺骗我 说:这里,是由冰构成的 其实不假, 人少的地方寒冷 人多的地方冷漠 没有一朵花的死亡能让人驻足 那些新的油井废弃了 更不必说那片老矿山 沉默着,摇摆着,沉寂了 只是那青天似墨 我深沉的灵魂 流连于你的双眼 明亮如星 纵使那不明...
31 “刘队长,刘二宝,放下枪,放松点。”陈深却摊开两只手,掌心翻向下,做了个安抚的手势,嘴角一弯露出个浅浅的酒窝。 于胖子也像是为事情的神走向惊呆了,连他顶着唐山海太阳穴的枪口早就松了劲儿也没发现,当然如果不是此时的唐山海浑身僵冷大脑一片空白,注意力全被拉到了刘二宝和陈深...
有趣,以我目前的认知边界来说,是对一个人的最高评价。 若是立刻让我想一些有趣的人出来,我第一个大概会想到王小波,因为正是在我开始阅读的那些年,当读到他《黄金时代》的那句话时,让我觉得他可爱有趣,一副“想起你的模样我的丑脸就泛起了微笑”的中年大叔模样。 还有:“那一天我二十一...
序言——不知道什么时候,自己有了想写东西的想法,我很喜欢书,但真正看的却不多,在一个城市待过,我都会买上几本,每次都没看完过,说起来很惭愧,我很羡慕那些文笔好的人,也希望自己可以像他们一样。我是个性格内向的人,不善言语,也不会表达,碰见陌生人更是说不出话来,有时候想到一些东...【IOS学习】http异步文件上传和下载及进度指示
文件下载和进度
nodejs服务端下载图片
先改造一下我们的服务端程序,来下载一张图片,代码如下
//下载返回文件流
function download(req,res){
var downloadFilePath = &./1.jpg&;
var filename = path.basename(downloadFilePath);
var filesize = fs.readFileSync(downloadFilePath).
res.setHeader('Content-Disposition','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);
case &/download& : download(req,res);
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 *)
NSLog(@&response:%@&,resp);
//接收响应
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@&=================didReceiveData=================&);
NSLog(@&data.length:%lu&,(unsigned long)data.length);
完成请求后打印出的结果如下:
21:48:50.694 network-demo[5] =================request redirectResponse=================
21:48:50.696 network-demo[5] request: { URL: https://localhost:8001/download }
21:48:50.706 network-demo[5] =================didReceiveResponse=================
21:48:50.706 network-demo[5] response: { URL: https://localhost:8001/download } { status code: 200, headers {
Connection = &keep-alive&;
&Content-Disposition& = &filename=1.jpg&;
&Content-Length& = 19557;
&Content-Type& = &application/octet-stream&;
Date = &Thu, 11 Feb :50 GMT&;
21:48:50.706 network-demo[5] =================didReceiveData=================
21:48:56.148 network-demo[5] data.length:19557
21:48:56.148 network-demo[5] =================connectionDidFinishLoading=================
可以看出:服务端在resqonse头中加入了content-length信息,告知整个流的大小,不过因为图片文件本身 较小,所以并没有分包,因此didReceiveData方法只调用了一次就完成了文件传递。大家可以试着修改下服务端返回的文件, 改为一个较大点的文件来试一次,这里我改成一个稍微大一些的图片,一张我自己hhkb键盘的美图~
var downloadFilePath = &./IMG_0222.jpg&;再试着发一次请求:
22:09:14.088 network-demo[5] =================request redirectResponse=================
22:09:14.089 network-demo[5] request: { URL: https://localhost:8001/download }
22:09:14.118 network-demo[5] =================didReceiveResponse=================
22:09:14.119 network-demo[5] response: { URL: https://localhost:8001/download } { status code: 200, headers {
Connection = &keep-alive&;
&Content-Disposition& = &filename=IMG_0222.jpg&;
&Content-Length& = 1265302;
&Content-Type& = &application/octet-stream&;
Date = &Thu, 11 Feb :14 GMT&;
22:09:14.119 network-demo[5] =================didReceiveData=================
22:09:14.119 network-demo[5] data.length:65536
22:09:14.120 network-demo[5] =================didReceiveData=================
22:09:14.120 network-demo[5] data.length:65536
22:09:14.121 network-demo[5] =================didReceiveData=================
22:09:14.121 network-demo[5] data.length:65536
22:09:14.122 network-demo[5] =================didReceiveData=================
22:09:14.122 network-demo[5] data.length:131072
22:09:14.123 network-demo[5] =================didReceiveData=================
22:09:14.123 network-demo[5] data.length:132000
22:09:14.123 network-demo[5] =================didReceiveData=================
22:09:14.124 network-demo[5] data.length:392288
22:09:14.124 network-demo[5] =================didReceiveData=================
22:09:14.124 network-demo[5] data.length:65536
22:09:14.124 network-demo[5] =================didReceiveData=================
22:09:14.125 network-demo[5] data.length:65536
22:09:14.125 network-demo[5] =================didReceiveData=================
22:09:14.125 network-demo[5] data.length:65536
22:09:14.125 network-demo[5] =================didReceiveData=================
22:09:14.125 network-demo[5] data.length:65536
22:09:14.126 network-demo[5] =================didReceiveData=================
22:09:14.126 network-demo[5] data.length:151190
22:09:14.126 network-demo[5] =================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 =
//文件大小
form.maxFieldsSize = 10 * 1024 * 1024;
form.parse(req, function (err, fields, files) {
res.send(err);
// 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() +
//移动的文件目录
var newPath = form.uploadDir + avatarN
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- boundary=YY,boundary用于标识边界,可以自定义,使用时前面需要加上两个&,例如:&&YY&
我们在iOS上传文件时需要这样设置请求头
/** 设置请求头 */
// 请求体的长度
[request setValue:[NSString stringWithFormat:@&%zd&, body.length] forHTTPHeaderField:@&Content-Length&];
// 声明这个POST请求是个文件上传
[request setValue:@&multipart/form- 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- 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- 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);
//接收响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@&=================didReceiveResponse=================&);
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)
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- boundary=YY',
connection: 'keep-alive',
accept: '*/*',
'user-agent': 'network-demo/1 CFNetwork/758.0.2 Darwin/14.5.0',
'content-length': ';,
'accept-language': 'en-us',
'accept-encoding': 'gzip, deflate' }
客户端日志:
13:05:07.330 network-demo[5] =================request redirectResponse=================
13:05:07.331 network-demo[5] request: { URL: https://localhost:8001/upload }
13:05:07.339 network-demo[5] =================totalBytesWritten=================
13:05:07.339 network-demo[5] didSendBodyData:32768,totalBytesWritten:32768,totalBytesExpectedToWrite:1265418
13:05:07.339 network-demo[5] 上传进度2%
13:05:07.339 network-demo[5] =================totalBytesWritten=================
13:05:07.339 network-demo[5] didSendBodyData:32768,totalBytesWritten:65536,totalBytesExpectedToWrite:1265418
13:05:07.339 network-demo[5] 上传进度5%
13:05:07.340 network-demo[5] =================totalBytesWritten=================
13:05:07.340 network-demo[5] didSendBodyData:32768,totalBytesWritten:98304,totalBytesExpectedToWrite:1265418
13:05:07.340 network-demo[5] 上传进度7%
13:05:07.340 network-demo[5] =================totalBytesWritten=================
13:05:07.340 network-demo[5] didSendBodyData:32768,totalBytesWritten:131072,totalBytesExpectedToWrite:1265418
13:05:07.340 network-demo[5] 上传进度10%
13:05:07.340 network-demo[5] =================totalBytesWritten=================
13:05:07.341 network-demo[5] didSendBodyData:32768,totalBytesWritten:163840,totalBytesExpectedToWrite:1265418
13:05:07.341 network-demo[5] 上传进度12%
13:05:07.341 network-demo[5] =================totalBytesWritten=================
13:05:07.341 network-demo[5] didSendBodyData:32768,totalBytesWritten:196608,totalBytesExpectedToWrite:1265418
13:05:07.341 network-demo[5] 上传进度15%
13:05:07.341 network-demo[5] =================totalBytesWritten=================
13:05:07.341 network-demo[5] didSendBodyData:32768,totalBytesWritten:229376,totalBytesExpectedToWrite:1265418
13:05:07.342 network-demo[5] 上传进度18%
13:05:07.342 network-demo[5] =================totalBytesWritten=================
13:05:07.342 network-demo[5] didSendBodyData:32768,totalBytesWritten:262144,totalBytesExpectedToWrite:1265418
13:05:07.342 network-demo[5] 上传进度20%
13:05:07.342 network-demo[5] =================totalBytesWritten=================
13:05:07.342 network-demo[5] didSendBodyData:32768,totalBytesWritten:294912,totalBytesExpectedToWrite:1265418
13:05:07.342 network-demo[5] 上传进度23%
13:05:07.343 network-demo[5] =================totalBytesWritten=================
13:05:07.343 network-demo[5] didSendBodyData:32768,totalBytesWritten:327680,totalBytesExpectedToWrite:1265418
13:05:07.343 network-demo[5] 上传进度25%
13:05:07.343 network-demo[5] =================totalBytesWritten=================
13:05:07.343 network-demo[5] didSendBodyData:32768,totalBytesWritten:360448,totalBytesExpectedToWrite:1265418
13:05:07.343 network-demo[5] 上传进度28%
13:05:07.343 network-demo[5] =================totalBytesWritten=================
13:05:07.343 network-demo[5] didSendBodyData:32768,totalBytesWritten:393216,totalBytesExpectedToWrite:1265418
13:05:07.344 network-demo[5] 上传进度31%
13:05:07.344 network-demo[5] =================totalBytesWritten=================
13:05:07.344 network-demo[5] didSendBodyData:32768,totalBytesWritten:425984,totalBytesExpectedToWrite:1265418
13:05:07.344 network-demo[5] 上传进度33%
13:05:07.354 network-demo[5] =================totalBytesWritten=================
13:05:07.354 network-demo[5] didSendBodyData:32768,totalBytesWritten:458752,totalBytesExpectedToWrite:1265418
13:05:07.354 network-demo[5] 上传进度36%
13:05:07.354 network-demo[5] =================totalBytesWritten=================
13:05:07.354 network-demo[5] didSendBodyData:32768,totalBytesWritten:491520,totalBytesExpectedToWrite:1265418
13:05:07.354 network-demo[5] 上传进度38%
13:05:07.354 network-demo[5] =================totalBytesWritten=================
13:05:07.354 network-demo[5] didSendBodyData:32768,totalBytesWritten:524288,totalBytesExpectedToWrite:1265418
13:05:07.355 network-demo[5] 上传进度41%
13:05:07.355 network-demo[5] =================totalBytesWritten=================
13:05:07.355 network-demo[5] didSendBodyData:32768,totalBytesWritten:557056,totalBytesExpectedToWrite:1265418
13:05:07.355 network-demo[5] 上传进度44%
13:05:07.355 network-demo[5] =================totalBytesWritten=================
13:05:07.355 network-demo[5] didSendBodyData:32768,totalBytesWritten:589824,totalBytesExpectedToWrite:1265418
13:05:07.355 network-demo[5] 上传进度46%
13:05:07.356 network-demo[5] =================totalBytesWritten=================
13:05:07.356 network-demo[5] didSendBodyData:32768,totalBytesWritten:622592,totalBytesExpectedToWrite:1265418
13:05:07.356 network-demo[5] 上传进度49%
13:05:07.356 network-demo[5] =================totalBytesWritten=================
13:05:07.356 network-demo[5] didSendBodyData:32768,totalBytesWritten:655360,totalBytesExpectedToWrite:1265418
13:05:07.356 network-demo[5] 上传进度51%
13:05:07.356 network-demo[5] =================totalBytesWritten=================
13:05:07.357 network-demo[5] didSendBodyData:32768,totalBytesWritten:688128,totalBytesExpectedToWrite:1265418
13:05:07.357 network-demo[5] 上传进度54%
13:05:07.357 network-demo[5] =================totalBytesWritten=================
13:05:07.357 network-demo[5] didSendBodyData:32768,totalBytesWritten:720896,totalBytesExpectedToWrite:1265418
13:05:07.357 network-demo[5] 上传进度56%
13:05:07.357 network-demo[5] =================totalBytesWritten=================
13:05:07.357 network-demo[5] didSendBodyData:32768,totalBytesWritten:753664,totalBytesExpectedToWrite:1265418
13:05:07.357 network-demo[5] 上传进度59%
13:05:07.358 network-demo[5] =================totalBytesWritten=================
13:05:07.358 network-demo[5] didSendBodyData:32768,totalBytesWritten:786432,totalBytesExpectedToWrite:1265418
13:05:07.358 network-demo[5] 上传进度62%
13:05:07.358 network-demo[5] =================totalBytesWritten=================
13:05:07.358 network-demo[5] didSendBodyData:32768,totalBytesWritten:819200,totalBytesExpectedToWrite:1265418
13:05:07.359 network-demo[5] 上传进度64%
13:05:07.359 network-demo[5] =================totalBytesWritten=================
13:05:07.359 network-demo[5] didSendBodyData:32768,totalBytesWritten:851968,totalBytesExpectedToWrite:1265418
13:05:07.359 network-demo[5] 上传进度67%
13:05:07.359 network-demo[5] =================totalBytesWritten=================
13:05:07.359 network-demo[5] didSendBodyData:32768,totalBytesWritten:884736,totalBytesExpectedToWrite:1265418
13:05:07.359 network-demo[5] 上传进度69%
13:05:07.359 network-demo[5] =================totalBytesWritten=================
13:05:07.360 network-demo[5] didSendBodyData:32768,totalBytesWritten:917504,totalBytesExpectedToWrite:1265418
13:05:07.360 network-demo[5] 上传进度72%
13:05:07.360 network-demo[5] =================totalBytesWritten=================
13:05:07.360 network-demo[5] didSendBodyData:32768,totalBytesWritten:950272,totalBytesExpectedToWrite:1265418
13:05:07.360 network-demo[5] 上传进度75%
13:05:07.360 network-demo[5] =================totalBytesWritten=================
13:05:07.360 network-demo[5] didSendBodyData:32768,totalBytesWritten:983040,totalBytesExpectedToWrite:1265418
13:05:07.361 network-demo[5] 上传进度77%
13:05:07.374 network-demo[5] =================totalBytesWritten=================
13:05:07.375 network-demo[5] didSendBodyData:32768,totalBytesWritten:1015808,totalBytesExpectedToWrite:1265418
13:05:07.375 network-demo[5] 上传进度80%
13:05:07.375 network-demo[5] =================totalBytesWritten=================
13:05:07.375 network-demo[5] didSendBodyData:32768,totalBytesWritten:1048576,totalBytesExpectedToWrite:1265418
13:05:07.375 network-demo[5] 上传进度82%
13:05:07.375 network-demo[5] =================totalBytesWritten=================
13:05:07.375 network-demo[5] didSendBodyData:32768,totalBytesWritten:1081344,totalBytesExpectedToWrite:1265418
13:05:07.375 network-demo[5] 上传进度85%
13:05:07.375 network-demo[5] =================totalBytesWritten=================
13:05:07.376 network-demo[5] didSendBodyData:32768,totalBytesWritten:1114112,totalBytesExpectedToWrite:1265418
13:05:07.376 network-demo[5] 上传进度88%
13:05:07.376 network-demo[5] =================totalBytesWritten=================
13:05:07.376 network-demo[5] didSendBodyData:32768,totalBytesWritten:1146880,totalBytesExpectedToWrite:1265418
13:05:07.376 network-demo[5] 上传进度90%
13:05:07.376 network-demo[5] =================totalBytesWritten=================
13:05:07.376 network-demo[5] didSendBodyData:32768,totalBytesWritten:1179648,totalBytesExpectedToWrite:1265418
13:05:07.376 network-demo[5] 上传进度93%
13:05:07.377 network-demo[5] =================totalBytesWritten=================
13:05:07.377 network-demo[5] didSendBodyData:32768,totalBytesWritten:1212416,totalBytesExpectedToWrite:1265418
13:05:07.377 network-demo[5] 上传进度95%
13:05:07.377 network-demo[5] =================totalBytesWritten=================
13:05:07.377 network-demo[5] didSendBodyData:32768,totalBytesWritten:1245184,totalBytesExpectedToWrite:1265418
13:05:07.377 network-demo[5] 上传进度98%
13:05:07.377 network-demo[5] =================totalBytesWritten=================
13:05:07.377 network-demo[5] didSendBodyData:20234,totalBytesWritten:1265418,totalBytesExpectedToWrite:1265418
13:05:07.378 network-demo[5] 上传进度100%
13:05:07.404 network-demo[5] =================didReceiveResponse=================
13:05:07.405 network-demo[5] response: { URL: https://localhost:8001/upload } { status code: 200, headers {
Connection = &keep-alive&;
Date = &Fri, 12 Feb :07 GMT&;
&Transfer-Encoding& = I
13:05:07.405 network-demo[5] =================didReceiveData=================
13:05:07.405 network-demo[5] data.length:28
13:05:07.405 network-demo[5] data:{
status = 1;
13:05:07.405 network-demo[5] =================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的,当文件太小不会进入这个委托方法。

我要回帖

更多关于 美篇如何上传音频 的文章

 

随机推荐