使用NSURLSession,NSURLSessionUploadTask上传文件

  • Post author:
  • Post category:其他
和我们的NSURLConnection的方法差不多,只不过把设置HTTPBody换了个地方http://blog.csdn.net/baohanqing/article/details/51171947
//
//  ViewController.m
//  NSURLSeesionUpload
//
//  Created by hq on 16/4/18.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "ViewController.h"

//boundary
#define HQBoundary @"com.hq"

//换行
#define HQNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]

//将字符串编码
#define HQEncode(string) [string dataUsingEncoding:NSUTF8StringEncoding]

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *urlString=@"http://120.25.226.186:32812/upload";
    
    NSString *name=@"file";
    
    NSString *fileName=@"flieName";
    
    NSString *fileMIMEType=@"image/jpeg";
    
    NSString *username=@"hq";

    
    NSURLSession *session=[NSURLSession sharedSession];
    
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    request.HTTPMethod=@"POST";
    
    //设置请求头
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",HQBoundary] forHTTPHeaderField:@"Content-Type"];
    
    //设置请求体
    NSMutableData *body=[NSMutableData data];
    
    
    //分割线
    [body appendData:HQEncode(@"--")];
    [body appendData:HQEncode(HQBoundary)];
    [body appendData:HQNewLine];
    
    
    //文件参数名
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@",name,fileName]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:HQNewLine];
    
    //文件类型
    [body appendData:[[NSString stringWithFormat:@"Content-Type:%@",fileMIMEType] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:HQNewLine];
    
    
    //文件数据
    [body appendData:HQNewLine];
    UIImage *image=[UIImage imageNamed:@"2"];
    [body appendData:UIImagePNGRepresentation(image)];
    [body appendData:HQNewLine];
    
    //非文件参数
    
    //分割线
    [body appendData:HQEncode(@"--")];
    [body appendData:HQEncode(HQBoundary)];
    [body appendData:HQNewLine];
    
    
    //参数名
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@",username] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:HQNewLine];
    
    [body appendData:HQNewLine];
    [body appendData:HQEncode(@"hq")];
    [body appendData:HQNewLine];
    
    
    //结束标记
    [body appendData:HQEncode(@"--")];
    [body appendData:HQEncode(HQBoundary)];
    [body appendData:HQEncode(@"--")];
    [body appendData:HQNewLine];
    
    NSURLSessionUploadTask *task=[session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
    }];
    
    [task resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


版权声明:本文为baohanqing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。