-2
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSString *imagePostUrl = [NSString stringWithFormat:@"www.abc.com?"];
NSDictionary *parameters = @{@"device_id":string,@"device_phone":[[NSUserDefaults standardUserDefaults] objectForKey:@"SerialNumber"]};

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:data name:@"uploaded_file" fileName:ext mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success: ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[[NSOperationQueue mainQueue] addOperation:op];

I am getting images from image in images path in mobile , I am sending all the images with parameters to the server. Here sending images are one by one with parameters to the server hoe can i send this . How can I achieve this? Please help me. Thanks in advance.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • 1
    We cannot help unless you show the code you wrote. And we need to know what error you received when it doesn't work. Also, when is the server-side written in? Are you using FTP or HTTP as transport? You need a lot more details in your question for anyone to help you. – Black Frog Apr 18 '15 at 12:23
  • Please submit your code and what you did so we can help you – Boda Apr 18 '15 at 12:30

2 Answers2

0

A normal way to upload images is to use a multi-part form encoding. Read this answer about uploading images using multi-part form data. If you use AFNetworking, it could be done with Multi-Part Request.

With JSON you could use a simple base64 encoded strings:

UIImage *image  = [[UIImage alloc] init]; //your image here
NSData *data = UIImagePNGRepresentation(image);
NSString *base64string = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

Do this for every image and send an array of strings in JSON format. On the server side you need to decode this strings to images. But it is not a good solution.

Community
  • 1
  • 1
Vlad
  • 7,199
  • 2
  • 25
  • 32
0

Yes,

I would suggest the same.

AFNetworking will do that for you like this:

-(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];
}
Scar
  • 3,460
  • 3
  • 26
  • 51
Muhammad Ahsan
  • 297
  • 2
  • 15