NSURLConnection文件上传-底层拼接出一个文件

#import "ViewController.h"

#define     ZYXBoundary             @"520it"
#define     ZYXEncode(string)       [string dataUsingEncoding:NSUTF8StringEncoding]
#define     ZYXNewLine              [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建请求
    NSURL *url = [NSURL URLWithString:@"http://www.example.com:8080/upload"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 设置请求头(告诉告诉服务器,这是一个文件上传的请求)
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", ZYXBoundary]
   forHTTPHeaderField:@"Content-Type"];
    
    // 设置请求体
    NSMutableData *body = [NSMutableData data];
    
    // 文件参数
    /*
     --分割线\r\n
     Content-Disposition: form-data; name="参数名"; filename="文件名"\r\n
     Content-Type: 文件的MIMEType\r\n
     \r\n
     文件数据
     \r\n
     */
    
    // 分割线
    [body appendData:ZYXEncode(@"--")];
    [body appendData:ZYXEncode(ZYXBoundary)];
    [body appendData:ZYXNewLine];
    
    // 文件参数名
    [body appendData:ZYXEncode([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"test.png\""])];
    [body appendData:ZYXNewLine];
    
    // 文件的类型
    [body appendData:ZYXEncode([NSString stringWithFormat:@"Content-Type: image/png"])];
    [body appendData:ZYXNewLine];
    
    // 文件数据
    [body appendData:ZYXNewLine];
    // UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)
    UIImage *image = [UIImage imageNamed:@"placeholder"];
    [body appendData:UIImagePNGRepresentation(image)];
    // [body appendData:[NSData dataWithContentsOfFile:@"/Users/zhaoyingxin/Desktop/test.png"]];
    [body appendData:ZYXNewLine];
    
    // 非文件参数
    /*
     --分割线\r\n
     Content-Disposition: form-data; name="参数名"\r\n
     \r\n
     参数值
     \r\n
     */
    // 分割线
    [body appendData:ZYXEncode(@"--")];
    [body appendData:ZYXEncode(ZYXBoundary)];
    [body appendData:ZYXNewLine];
    
    // 参数名
    [body appendData:ZYXEncode([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\""])];
    [body appendData:ZYXNewLine];
    
    // 参数值
    [body appendData:ZYXNewLine];
    [body appendData:ZYXEncode(@"solozyx")];
    [body appendData:ZYXNewLine];
    
    // 结束标记
    /*
     --分割线--\r\n
     */
    [body appendData:ZYXEncode(@"--")];
    [body appendData:ZYXEncode(ZYXBoundary)];
    [body appendData:ZYXEncode(@"--")];
    [body appendData:ZYXNewLine];
    
    request.HTTPBody = body;
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
                        }];
}

//2016-08-07 15:33:09.963 文件上传[54746:831242] {
//    success : 上传成功
//}

@end

你可能感兴趣的:(NSURLConnection文件上传-底层拼接出一个文件)