使用restkit上传多张图片?
Posted
技术标签:
【中文标题】使用restkit上传多张图片?【英文标题】:uploade multiple image using restkit? 【发布时间】:2016-02-18 06:08:28 【问题描述】:我正在使用 REST 服务的 restkit 库。我不知道如何发送多个图像,我可以发送单个图像,我不知道。
[LoadingView showInView:[[UIApplication sharedApplication].delegate window]];
NSString *url=@"/upload";
NSDictionary *dic=@@"userId":[self.appUtil getDataForKey:@"USERID"];
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil method:RKRequestMethodPOST path:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:UIImagePNGRepresentation(imgProfile.image)
name:@"file"
fileName:@"photo.png"
mimeType:@"image/png"];
];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSString* userString=operation.HTTPRequestOperation.responseString;
NSLog(@"%@",userString);
NSError* error = nil;
[LoadingView removeFromView:[[UIApplication sharedApplication].delegate window]];
StatusInnner *state=[[StatusInnner alloc]initWithString:userString error:&error];
if (state!=nil)
if ([state.status.status isEqualToString:@"200"])
[self.appUtil createSimpleAlertView:self title:string_title_success
message:state.status.msg];
else
[self.appUtil createSimpleAlertView:self title:string_title_success
message:state.status.msg];
【问题讨论】:
检查我的答案,如果您需要任何帮助,请告诉我 【参考方案1】:首先将所有图像数据添加到数组中。然后像这样在表单数据上创建一个循环
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil method:RKRequestMethodPOST path:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
for (int i=0;i<yourImagesArray.count;i++)
UIImage *image = [yourImagesArray objectAtIndex:i];
[formData appendPartWithFileData:UIImagePNGRepresentation(image)
name:@"file"
fileName:@"photo.png"
mimeType:@"image/png"];
];
【讨论】:
我会检查并通知您,以上是关于使用restkit上传多张图片?的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat 上的 HTTP/1.1 200 213 使用 RESTKit 上传 jpeg 文件