1

I am trying to upload an image to a server (that is already built) and I am getting errors like Request has timed out. Other methods of sending text and fetch data from the server are working properly. However, sending an image I found it hard to do it.

I am using the following code at the moment:

-(void)uploadImage:(NSData*)image callbackBlock: (void (^)(BOOL success)) callbackBlock
{
    NSString *path = [NSString stringWithFormat:@"upload"];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"image", nil];
    [params addEntriesFromDictionary:self.sessionManager.authParameters];



    NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData){


        [formData appendPartWithFormData:image name:@"Image"];

    }];

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"!!!Response object: %@",responseObject);


        callbackBlock(YES);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Failure: %@",error.description);

        callbackBlock(NO);
    }];

    [self enqueueHTTPRequestOperation:operation];
}

Do you have any idea what the problem is? Can you give me some suggestions or possible errors on the above code.

Thank you very much.

Tzegenos
  • 837
  • 12
  • 16

1 Answers1

0

You can send your image as a base64 encoded text... This should work. You can use this category to create base64 encoded image:

https://github.com/l4u/NSData-Base64

Balazs Nemeth
  • 2,333
  • 19
  • 29
  • Yes but in server side I am using other type of data (multipart form). Is your suggestion compatible with the encoding in server side? – Tzegenos Oct 31 '13 at 19:18
  • This can be useful for you: http://stackoverflow.com/questions/15410689/iphone-upload-multipart-file-using-afnetworking – Balazs Nemeth Nov 01 '13 at 20:44