您的位置:首页 > 移动开发 > IOS开发

IOS文件上传

2016-02-26 17:03 435 查看
IOS文件上传

 

[objc] view
plaincopy





//  

//  MJViewController.m  

//  02.Post上传  

//  

//  Created by apple on 14-4-29.  

//  Copyright (c) 2014年 itcast. All rights reserved.  

//  

  

#import "MJViewController.h"  

#import "UploadFile.h"  

  

@interface MJViewController ()  

  

@end  

  

@implementation MJViewController  

  

- (void)viewDidLoad  

{  

    [super viewDidLoad];  

  

    UploadFile *upload = [[UploadFile alloc] init];  

      

    NSString *urlString = @"http://localhost/upload.php";  

      

    NSString *path = [[NSBundle mainBundle] pathForResource:@"头像1.png" ofType:nil];  

    NSData *data = [NSData dataWithContentsOfFile:path];  

      

    [upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data];  

}  

  

@end  

[objc] view
plaincopy





//  

//  UploadFile.m  

//  02.Post上传  

//  

//  Created by apple on 14-4-29.  

//  Copyright (c) 2014年 itcast. All rights reserved.  

//  

  

#import "UploadFile.h"  

  

@implementation UploadFile  

// 拼接字符串  

static NSString *boundaryStr = @"--";   // 分隔字符串  

static NSString *randomIDStr;           // 本次上传标示字符串  

static NSString *uploadID;              // 上传(php)脚本中,接收文件字段  

  

- (instancetype)init  

{  

    self = [super init];  

    if (self) {  

        randomIDStr = @"itcast";  

        uploadID = @"uploadFile";  

    }  

    return self;  

}  

  

#pragma mark - 私有方法  

- (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile  

{  

    NSMutableString *strM = [NSMutableString string];  

      

    [strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];  

    [strM appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID, uploadFile];  

    [strM appendFormat:@"Content-Type: %@\n\n", mimeType];  

      

    NSLog(@"%@", strM);  

    return [strM copy];  

}  

  

- (NSString *)bottomString  

{  

    NSMutableString *strM = [NSMutableString string];  

      

    [strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];  

    [strM appendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];  

    [strM appendString:@"Submit\n"];  

    [strM appendFormat:@"%@%@--\n", boundaryStr, randomIDStr];  

      

    NSLog(@"%@", strM);  

    return [strM copy];  

}  

  

#pragma mark - 上传文件  

- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data  

{  

    // 1> 数据体  

    NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"];  

    NSString *bottomStr = [self bottomString];  

      

    NSMutableData *dataM = [NSMutableData data];  

    [dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];  

    [dataM appendData:data];  

    [dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];  

      

    // 1. Request  

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];  

      

    // dataM出了作用域就会被释放,因此不用copy  

    request.HTTPBody = dataM;  

      

    // 2> 设置Request的头属性  

    request.HTTPMethod = @"POST";  

      

    // 3> 设置Content-Length  

    NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];  

    [request setValue:strLength forHTTPHeaderField:@"Content-Length"];  

      

    // 4> 设置Content-Type  

    NSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];  

    [request setValue:strContentType forHTTPHeaderField:@"Content-Type"];  

      

    // 3> 连接服务器发送请求  

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  

          

        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  

        NSLog(@"%@", result);  

    }];  

}  

  

  

  

@end  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: