ios網路學習------10 原生API檔案上傳

NS西北風發表於2014-07-03

使用原生態的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


相關文章