0
votes

I tried to use this to upload user information + image to data base :

- (void)Inscription_withImage:(NSArray *)value :(NSData *)data completion:(void (^)( NSString * retour))block{


    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"data[User][username]"] = @"Jack";
    params[@"data[User][password]"] = @"Jack";
    params[@"data[User][email]"] = @"Jack";
    params[@"data[User][first_name]"] = @"Jack";
    params[@"data[User][last_name]"] = @"Jack";
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [mgr POST:@"http://  /Messenger/services/messenger_register" parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    [formData appendPartWithFileData:data name:@"data[User][image]" fileName:@"image.jpg" mimeType:@"image/jpg"];
     }
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@"upload success!----%@", responseObject);
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"upload failed!----%@", error);
      }];


  }

when i added this :

[formData appendPartWithFileData:data name:@"data[User][image]" fileName:@"image.jpg" mimeType:@"image/jpg"];

i found this problem:

upload failed!----Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x176d25c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Please i need your help

1

1 Answers

0
votes

The Image should be converted to NSData then encoded in base64 to compress its size

 NSData * representation = UIImageJPEGRepresentation(image, 1.0f);
    NSString * imageBase64 =[NSString base64StringFromData:representation];
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                userID,@"id",
                                imageBase64,@"img_data",
                                [NSString stringWithFormat:@"user_%@.jpg",userID],@"avatar_url",nil];