0

I want to upload an image to a webservice from iOS. In web service image parameter datatype is File. I want to send a thumb of a image that user taking from his phone camera. Can anyone says how to send a thumb image from iOS app to a web service.

Thanks.

iDia
  • 1,397
  • 8
  • 25
  • 44
  • I imagine it would involve a POST request. Without a **far** more specific question though, no one will be able to be more detailed. – borrrden Jun 10 '13 at 03:00
  • I want to know the way to upload the image using POST request. – iDia Jun 10 '13 at 03:02
  • I already convert it into base64 format,,, but what should I do further from that point – iDia Jun 10 '13 at 03:04
  • @iDia Did you look at the answer to the question above? It seems to have exactly what you want. – borrrden Jun 10 '13 at 03:09
  • yes I checked but I can't understand what is that boundary variable. Do I need to send all parameters of that particular link according to POST? or only the image should send in POST way? – iDia Jun 10 '13 at 03:17
  • There is already a duplicate question on this topic http://stackoverflow.com/questions/5422028/objective-c-how-to-upload-image-and-text-using-http-post You might find this link helpful too. http://insanelycrazy.net/development/faces/viewarticle.xhtml?id=6 – aksh1t Jun 10 '13 at 03:10

2 Answers2

4

With AFNetworking you can do it by:

NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourImage.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"MainMedia" fileName:@"MainMedia" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation start];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Prateek Prem
  • 1,544
  • 11
  • 14
2

If you are using ASIHttpRequest Library look at this code:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:yourRequestURL]];

[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 
[request addRequestHeader:@"Content-Type" value:@"application/json"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,     NSUserDomainMask, YES);
NSString *savedImagePath = [NSString stringWithFormat:@"%@/%@/%@",[paths objectAtIndex:0],folder,imageName];

if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath])
    [request setFile:savedImagePath forKey:@"imgfile"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFailSelector:@selector(requestFailed:)];

I hope it will help you.

sunkehappy
  • 8,970
  • 5
  • 44
  • 65
Prateek Prem
  • 1,544
  • 11
  • 14