// // ViewController.m // 02-POST // // Created by jerry on 15/10/9. // Copyright (c) 2015年 jerry. All rights reserved. // #import "ViewController.h" #import "NSMutableURLRequest+Muitipart.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self postUpLoad]; } - (void)postUpLoad { // url NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithUrl:url andLoaclFilePath:[[NSBundle mainBundle] pathForResource:@"111.png" ofType:nil] andFileName:@"123.png"]; // connection [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 反序列化的一个处理 id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; NSLog(@"%@",result); }]; } @end
NSMutableURLRequest的分类:
.h
// // NSMutableURLRequest+Muitipart.h // 02-POST // // Created by jerry on 15/10/10. // Copyright (c) 2015年 jerry. All rights reserved. // #import <Foundation/Foundation.h> @interface NSMutableURLRequest (Muitipart) /** * 类方法 * * @param url 要上传的服务器地址 * @param loadFilePath 要上传的文件全路径 * @param fileName 保存到服务器的名称。 * */ + (instancetype)requestWithUrl:(NSURL *)url andLoaclFilePath:(NSString *)loadFilePath andFileName:(NSString *)fileName; @end
.m
// // NSMutableURLRequest+Muitipart.m // 02-POST // // Created by jerry on 15/10/10. // Copyright (c) 2015年 jerry. All rights reserved. // #import "NSMutableURLRequest+Muitipart.h" static NSString *boundary = @"ZPUpLoad"; @implementation NSMutableURLRequest (Muitipart) + (instancetype)requestWithUrl:(NSURL *)url andLoaclFilePath:(NSString *)loadFilePath andFileName:(NSString *)fileName { // request #warning -- 这里不是NSURLRequest 而是 NSMutableURLRequest 因为这个请求是可变请求。 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0f]; // 声明是哪种格式的请求,默认是GET。 request.HTTPMethod = @"POST"; /** \r\n--(可以随便写, 但是不能有中文)\r\n Content-Disposition: form-data; name="userfile(php脚本中用来读取文件的字段)"; filename="demo.json(要保存到服务器的文件名)"\r\n Content-Type: application/octet-stream(上传文件的类型)\r\n\r\n \r\n--(可以随便写, 但是不能有中文)\r\n */ // 数据体 拼接数据体 NSMutableData *dataM = [NSMutableData data]; // 1. \r\n--(可以随便写, 但是不能有中文)\r\n NSString *str = [NSString stringWithFormat:@"\r\n--%@\r\n",boundary]; [dataM appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; // 2. Content-Disposition: form-data; name="userfile(php脚本中用来读取文件的字段)"; filename="demo.json(要保存到服务器的文件名)"\r\n str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\" \r\n", fileName]; [dataM appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; // 3.Content-Type: application/octet-stream(上传文件的类型)\r\n\r\n str = @"Content-Type: application/octet-stream\r\n\r\n"; [dataM appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; // 4.要上传文件的二进制流 [dataM appendData:[NSData dataWithContentsOfFile:loadFilePath]]; // 5.\r\n--(可以随便写, 但是不能有中文)\r\n str = [NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]; [dataM appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; request.HTTPBody = dataM; /** Content-Length(文件的大小) 290 Content-Type multipart/form-data; boundary(分隔符)=(可以随便写, 但是不能有中文) */ // 设置请求头 NSString *headerStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request setValue:headerStr forHTTPHeaderField:@"Content-Type"]; return request; } @end
时间: 2024-10-10 13:39:31