4

I am totally new to iOS.I want to upload images to my server using AFNetworking. I tried it but it still not done. Please Help me, thanks in advance

user1548843
  • 670
  • 12
  • 20
Yagnesh Dobariya
  • 2,241
  • 19
  • 29

1 Answers1

5

try with this its work for me

-(void) uplodeImages{

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager   alloc] initWithBaseURL:[NSURL URLWithString:@"url where to upload images"]];
    NSData *imageData = UIImagePNGRepresentation([UIImage    imageNamed:@"cat1.png"]);
    NSDictionary *parameters = @{@"username": @"", @"password" : @""};
    AFHTTPRequestOperation *op = [manager POST:@"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"cat1.png" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];
}
Sachin Patil
  • 353
  • 1
  • 13
  • Ya, this is helpful for me. Thnaks Sachin Partil – Yagnesh Dobariya Feb 27 '15 at 11:47
  • none of the success or failure blocks are called, do you know why? – Septronic Nov 23 '15 at 18:18
  • @Septronic nice ans but i have a ques if you can help me out ..can i send **image(with key)**, **audio file(with key)** and string values (dict parameters) together using this code ..? **please help if you little time** – vaibhav May 26 '16 at 11:52