博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone完整版的http上传请求协议
阅读量:6924 次
发布时间:2019-06-27

本文共 3426 字,大约阅读时间需要 11 分钟。

完整版的iphone(XCODE),oc   http上传请求协议,一个完整的DEMO,提供给大家学习
//保存图片
- (void)saveImage
{
_alertview= [[UIAlertViewalloc]initWithTitle:nil
message:@"正在上传照片..."
   delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
//加载一个旋转的等待图标
activityView= [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame=CGRectMake(120.f,48.0f,37.0f,37.0f);//加入等待图标
[_alertviewaddSubview:activityView];
[activityViewstartAnimating];
[activityViewrelease];
[_alertviewshow];
NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"http://xx.xx.xx/xinupload.jsp?id=%@",[ABInfogetUserID]]];//请求服务器路径。
m_imageData=UIImagePNGRepresentation(myImageView.image);
//m_imageData = UIImageJPEGRepresentation(myImageView.image, 1.0);
NSData*postDateBoundary = [[NSStringstringWithFormat:@"-----------------------------7da36b1b4045c\r\n"]dataUsingEncoding:NSUTF8StringEncoding];
NSData*postDateBoundaryEnd = [[NSStringstringWithFormat:@"\r\n-----------------------------7da36b1b4045c--\r\n"]dataUsingEncoding:NSUTF8StringEncoding];
NSData*postDateHead = [[NSStringstringWithFormat:
@"Content-Disposition: form-data; name="File1\"; filename="a1.jpg"\r\nContent-Type: image/pjpeg\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData*postData = [NSMutableDatadataWithCapacity:[m_imageDatalength] ];
[postData appendData:postDateBoundary];
[postData appendData:postDateHead];
[postData appendData:m_imageData];
[postData appendData:postDateBoundaryEnd];
NSMutableURLRequest*urlRequest = [NSMutableURLRequestrequestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequestsetValue: [NSStringstringWithFormat:@"%@\r\n"
   ,@"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"] forHTTPHeaderField:@"Accept"];
[urlRequestsetValue:[NSStringstringWithFormat:@"%@\r\n",@"zh-CN"] forHTTPHeaderField:@"Accept-Language"];
[urlRequestsetValue:[NSStringstringWithFormat:@"multipart/form-data; boundary=---------------------------7da36b1b4045c"]   forHTTPHeaderField:@"Content-Type"];
NSString *length=[NSString stringWithFormat:@"%d",[m_imageData length] + [postDateBoundary length] + [postDateHead length]+ [postDateBoundaryEnd length]];
[urlRequestsetValue:[NSStringstringWithFormat:@"%@\r\n\r\n",length]forHTTPHeaderField:@"Content-Length"];
[urlRequestsetValue:[NSStringstringWithFormat:@"%@\r\n\r\n",@"keep-alive"]forHTTPHeaderField:@"Connection"];
[urlRequest setHTTPBody:postData];
NSURLConnection*theConnection = [[NSURLConnectionalloc]initWithRequest:urlRequestdelegate:self];
if (theConnection)
{
uploadDidSucceed=NO;
_webData= [[NSMutableDatadata]retain];//初始化一个数据流
}
}
// 开始WebServices API
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
      [_webData setLength: 0];
}
// 接受数据API
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    [_webData appendData:data];
}
//连接结束
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
     uploadDidSucceed=NO;
     [activityViewstopAnimating];
     [_alertviewdismissWithClickedButtonIndex:0animated:YES];
     [_alertviewrelease];
     UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"照片"
                                                                   message:@"设置成功"
                                                                    delegate:self
                                                         cancelButtonTitle:nil
                                otherButtonTitles:CONFIRM_TITLE, nil];
     [alert show];
     [alert release];
}
****这个是一个完整的上传组件,只要提供图片数据,剩下的拷贝粘贴就可以了 

转载地址:http://yhcjl.baihongyu.com/

你可能感兴趣的文章
Linux module 原理与实现
查看>>
Android自动化测试之使用java调用monkeyrunner
查看>>
Android Studio 使用中遇到的问题解决
查看>>
Linux下文件编程实验
查看>>
Javascript小技巧一箩筐
查看>>
SQLAlchemy 文档翻译(2)
查看>>
使用Ceph集群作为Kubernetes的动态分配持久化存储
查看>>
数据库SQL优化大总结之 百万级数据库优化方案
查看>>
双绞线
查看>>
nginx反向代理学习笔记
查看>>
习题:打印如下图
查看>>
puppet练习记录一
查看>>
mysql 实现读写分离详解
查看>>
本地YUM源配置-Redhat Enterprise 5.9
查看>>
CDH 5.7.0 安装文档
查看>>
小小的公共库,大大的耦合,你痛过吗?
查看>>
VDI序曲二十一 APP-V 4.6 SP1服务器端部署
查看>>
我的友情链接
查看>>
centos6.3 下kvm安装配置详细流程
查看>>
考题纠错
查看>>