首页 > 代码库 > ios网络学习------10 原生API文件上传

ios网络学习------10 原生API文件上传

使用原生态的api上传文件的实现:

#import "MainViewController.h"

@interface MainViewController ()
@property (weak, nonatomic) UIImageView *imageView;
@end

@implementation MainViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //定义Imageview并设置图像
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [imageView setFrame:CGRectMake(60, 20, 200, 200)];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(60, 240, 200, 40)];
    
    [button setTitle:@"upload" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(uploadImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

#pragma mark 上传文件(图像)
- (void)uploadImage
{
    NSLog(@"upload");
    //思路: 需要使用http的post方法上传文件
    //调用的url是http://localhost/~apple/itcast/upload.php
    //数据体的参数名:file
    
    //1建立URL
    NSURL *url = [NSURL URLWithString:@"http://localhost/~apple/itcast/upload.php"];
    //2建立NSMutableRequest
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //1)设置request的属性,设置方法
    [request setHTTPMethod:@"POST"];
    
    
    
    
    //2)设置数据体
    //1> 设置boundary的字符串,可以复用
    
    NSString *boundary = @"uploadBoundary";
    //2>头部字符串
    
    NSMutableString *startStr = [NSMutableString string];
    [startStr appendFormat:@"--%@\n", boundary];
    [startStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"upload.png\"\n"];
    [startStr appendString:@"Content-Type: image/png\n\n"];
    
    //3>尾部字符串
    NSMutableString *endStr = [NSMutableString string];
    [endStr appendFormat:@"--%@\n", boundary];
    [endStr appendString:@"Content-Disposition: form-data: name=\"submit\"\n\n"];
    [endStr appendString:@"Submit\n"];
    [endStr appendFormat:@"--%@--", boundary];
    
    //4>拼接数据体
    NSMutableData *bodyData = [NSMutableData data];
    [bodyData appendData:[startStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
    [bodyData appendData:imageData];
    [bodyData appendData:[endStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:bodyData];
    
    //5>指定Content-Type,在上传文件时,需要指定content-type和content-length
    NSString *contentStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentStr forKey:@"Content-Type"];
    
    //6>指定Content-Length
    NSInteger length = [bodyData length];
    [request setValue:[NSString stringWithFormat:@"%d", length] forKey:@"Content-Length"];
    
    
    
    
    //3使用NSURLConnection的同步方法上传文件,因为需要用户确认文件是否上传成功。
    //在使用http上传文件时,通常是有大小限制的。一般不会超过2M.
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *resultStr = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", resultStr);
}

@end