使用AFNetworking 2.0上传图像

这个我正在撞墙。 我想从库中selectUIImage并将其上传到服务器,就像使用<form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data"> 。 而不是成功,我得到了这个错误:

错误=错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(cocoa错误3840.)”(JSON文本没有开始数组或对象和选项允许片段不设置。)UserInfo = 0x145e5d90 {NSDebugDescription = JSON文本没有以数组或对象和选项开始,以允许片段未设置。}

我试过这种方式:

 -(void)uploadPhoto{ NSString *path = @"http://blabla.request.cfm"; NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9); int priv = self.isPrivate ? 1 : 0; NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData}; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { if(self.imageView.image){ [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; } } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"[UploadVC] success = %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"[UploadVC] error = %@", error); }]; [self blockView:self.view block:YES]; } 

但它不工作…服务器说,没有文件。 不知道如果encryption是错误的,MIMEtypes或什么?

也试过这个:

  [manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"[UploadVC] success = %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"[UploadVC] error = %@", error); }]; 

和这个:

  manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFormData:imageData name:@"photo"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"[UploadVC] success = %@", responseObject); [self blockView:self.view block:NO]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"[UploadVC] error response.object = %@", operation.responseObject); [self blockView:self.view block:NO]; }]; 

没有任何工作。 希望有人能帮助,因为我困在这里,在这里围绕着这个问题盘旋
TIA

编辑 :新的尝试
1)首先是多部分forms
2)创build上传任务
他们都没有为我工作,所以我仍然试图应付这个,但看不到任何解决scheme

我不确定哪个部分(我认为有些细节缺失)是有责任的,但我终于做到了:)在这里你去:

 -(void)uploadPhoto{ AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]]; NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5); NSDictionary *parameters = @{@"username": self.username, @"password" : self.password}; AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //do not put image inside parameters dictionary as I did, but append it! [formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@ ***** %@", operation.responseString, error); }]; [op start]; } 

奇迹般有效 :)

您可以使用像这样使用Swift AFNetworking上传图像…

  let compression = 0.5 let imageData = UIImageJPEGRepresentation("image", CGFloat(compression)) if imageData != nil{ var manager = AFHTTPRequestOperationManager() manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html", "application/json"]) as Set<NSObject> var dictParams = [ "familyId":"10000", "contentBody" : "Some body content for the test application", "name" : "the name/title", "typeOfContent":"photo" ] let url = "http://...." manager.POST(url, parameters: dictParams, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in //code formData.appendPartWithFileData(imageData, name: "file", fileName: "filename", mimeType: "image/png") }, success: { (operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in println(responseObject) }, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in println(error) }) } 
  UIImage *image = [UIImage imageNamed:@"decline_clicked.png"]; NSData *imageData = UIImageJPEGRepresentation(image,1); NSString *queryStringss = [NSString stringWithFormat:@"http://your server/uploadfile/"]; queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; [manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"fileName" fileName:@"decline_clicked.png" mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *dict = [responseObject objectForKey:@"Result"]; NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@ ***** %@", operation.responseString, error); }];