zoukankan      html  css  js  c++  java
  • 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 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];
    }
    ****这个是一个完整的上传组件,只要提供图片数据,剩下的拷贝粘贴就可以了 
  • 相关阅读:
    直线的中点Bresenham算法的实现
    使用git 将自己的本地文件git到github上面的完整过程
    利用Microsoft VC++6.0 的MFC 的绘图工具实现简单图形的绘制
    github常见操作和常见错误及其解决办法
    浅谈软件配置管理工具(github & SVN)
    为Github 托管项目的访问添加SSH keys
    jQuery:用 lightTreeview 实现树形分类菜单的功能 展开收缩分类代码
    程序设计7大面向对象设计原则
    计算机组成原理实验之模拟整机实验考核试题
    计算机组成原理实验之CPU组成与指令周期实验
  • 原文地址:https://www.cnblogs.com/top5/p/2402323.html
Copyright © 2011-2022 走看看