NSURLConnection实现文件上传和AFNetworking实现文件上传

请求的步骤分为4步

1、创建请求

2、设置请求头(告诉服务器这是一个文件上传的请求)

3、设置请求体

4、发送请求

NSURLConnection实现文件上传

// 1、创建请求
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: timeoutInterval:2.0f]; // 2、设置请求头(告诉服务器这是一个文件上传的请求)
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",XMGEncode(@"520it")] forHTTPHeaderField:@"Content-Type"]; // 3、设置请求体
// 开始标记
NSMutableData *body = [NSMutableData data];
[body appendData:XMGEncode(@"--")];
[body appendData:XMGEncode(@"520it")];
[body appendData:XMGNewLine]; // 文件参数
// 参数名和文件名
[body appendData:XMGEncode([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"test.png\""])];
[body appendData:XMGNewLine];
// 文件类型
[body appendData:XMGEncode([NSString stringWithFormat:@"Content-Type: image/png"])];
[body appendData:XMGNewLine];
// 文件数据
[body appendData:XMGNewLine];
UIImage *img = [UIImage imageNamed:@""];
[body appendData:UIImagePNGRepresentation(img)];
[body appendData:XMGNewLine]; // 非文件参数
[body appendData:XMGEncode(@"--")];
[body appendData:XMGEncode(@"520it")];
[body appendData:XMGNewLine];
// 参数名
[body appendData:XMGEncode([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\""])];
[body appendData:XMGNewLine];
// 参数值
[body appendData:XMGNewLine];
[body appendData:XMGEncode(@"jack")];
[body appendData:XMGNewLine]; // 结束标记
[body appendData:XMGEncode(@"--")];
[body appendData:XMGEncode(@"520it")];
[body appendData:XMGEncode(@"--")];
[body appendData:XMGNewLine]; request.HTTPBody = body; // 4、链接服务器发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];

AFNetworking实现文件上传

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"jack"} constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// 在这个block中设置需要上传的文件
// NSData *data = [NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/placeholder.png"];
// [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"]; // [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" error:nil]; } progress:^(NSProgress * _Nonnull uploadProgress) {
//
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
}];
 
上一篇:iOS沙盒机制介绍


下一篇:mahout 运行Twenty Newsgroups Classification实例