图片上传

单图片上传

BARequest *request = [BARequest POSTRequestWithPath:@"avatar" parameters:@{@"type" : @"avatar"}];
request.fileData = [BARequestFileData fileDataWithData:[NSData data] name:@"fileKey" fileName:@"fileName"];
[[[BAClient currentClient] performRequest:request] onComplete:^(id result, NSError *error) {
if (error) {
NSLog(@"文件上传出错");
} else {
NSLog(@"文件上传出错");
}
}];

多图片上传

NSURL *url = [NSURL URLWithString:@"http://qebaby.nowtime.com.cn/index.php/app/topic/uploads"];
BARequest *request = [BARequest POSTRequestWithURL:url parameters:nil];
request.contentType = BARequestContentTypeMultipart;
BARequestFileData *fileData1 = [BARequestFileData fileDataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]) name:@"image1" fileName:@"fileName1"];
BARequestFileData *fileData2 = [BARequestFileData fileDataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]) name:@"image2" fileName:@"fileName2"];
request.fileDatas = @[fileData1, fileData2];
[[[BAClient currentClient] performRequest:request] onComplete:^(id result, NSError *error) {
if (error) {
NSLog(@"文件上传出错");
} else {
NSLog(@"文件上传成功 %@", [result body]);
}
}];

注: 虽然BANetwroking提供多张图片上传,但作者并不建议使用,因为多图片上传即浪费用户流量又会使用户在上传的过程中等待时间很长,影响用户体验。浪费用户流量一说怎么理解呢?我举个例子,如果你一起上传3张图片,如果上传过程中上传到第三张时这时候突然网断了,这个时间整体就上传失败了。