标签:
// // UploadFile.h // 02.Post上传 // // Created by wyh on 15-1-29. // Copyright (c) 2015年 itcast. All rights reserved. // #import <Foundation/Foundation.h> @interface UploadFile : NSObject - (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data; @end
// // UploadFile.m // 02.Post上传 // // Created by why on 15-1-29. // Copyright (c) 2015年 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
// // MJViewController.m // 02.Post上传 // // Created by wyh on 15-1-29. // Copyright (c) 2015年 itcast. All rights reserved. // #import "ViewController.h" #import "UploadFile.h" @interface ViewController () @end @implementation ViewController - (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
标签:
原文地址:http://www.cnblogs.com/wangyinghui/p/4356719.html