iphone完整版的http上传请求协议

完整版的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 setLength0];

}

 

// 接受数据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_TITLEnil];

     [alert show];

     [alert release];

 

}

****这个是一个完整的上传组件,只要提供图片数据,剩下的拷贝粘贴就可以了
类别: 网络部分  查看评论

你可能感兴趣的:(iPhone)