通过表单上传图片一张,多张同时上传 --iOS可乐

一、先来写写一张的吧 

请求方式 post

-(void)upload{

NSString *urlStr = [NSString stringWithFormat:url];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:0 timeoutInterval:5.0f];

[self setRequest:request];

NSLog(@"开始上传");

NSURLSession  *session = [NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

      NSLog(@"Result -- %@",[[NSString alloc]initWithData:data         encoding:NSUTF8StringEncoding]);

}];

//发起

[dataTask resume];

}

-(void)setRequest:(NSMutableURLRequest *)request{

NSString *boundary = [NSString stringWithFormat:@"Boundary+%08X%08X",arc4random(),arc4random()];

//表单数据(这里可以放文本信息)

NSMutableDictionary *param = [[NSMutableDictionary alloc] init];

[param setValue:@"" forKey:@""];

[param setValue:@“" forKey:@""];

[param enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

NSMutableString *fieldStr = [NSMutableString string];

[fieldStr appendString:[NSString stringWithFormat:@"--%@\r\n", boundary]];

[fieldStr appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]];

[fieldStr appendString:[NSString stringWithFormat:@"%@", obj]];

[body appendData:[fieldStr dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

}];

//图片数据部分

NSMutableString *topStr = [NSMutableString string];

//取到图片

NSString *path = [[NSBundle mainBundle] pathForResource:@"001.jpg" ofType:nil];

NSData *data = [NSData dataWithContentsOfFile:path];

[topStr appendString:[NSString stringWithFormat:@"--%@\r\n", boundary]];

//这里具体怎么写,跟后台交流。。。

[topStr appendString:@"Content-Disposition: form-data; name= \"content2[]\"; filename=\"001.jpg\"\r\n"];

[topStr appendString:@"Content-Type:image/jpeg"];

[topStr appendString:@"Content-Transfer-Encoding: binary\r\n\r\n"];

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

[body appendData:data];

[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// 结束部分

NSString *bottomStr = [NSString stringWithFormat:@"--%@--", boundary];

/**拼装成格式:

--Boundary+72D4CD655314C423--

*/

//将body字符串转化为UTF8格式的二进制

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

// 设置请求类型为post请求

request.HTTPMethod = @"post";

//    [request setValue:appKey forHTTPHeaderField:@"appKey"];

//    [request setValue:appId forHTTPHeaderField:@"appId"];

[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];

// 设置request的请求体

request.HTTPBody = body;

// 设置头部数据,标明上传数据总大小,用于服务器接收校验

[request setValue:[NSString stringWithFormat:@"--%ld", body.length] forHTTPHeaderField:@"Content-Length"];

// 设置头部数据,指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。

[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];

}

多张上传的关注我,下一篇就是。。。。

你可能感兴趣的:(通过表单上传图片一张,多张同时上传 --iOS可乐)