0

I am using the following code to send a photo to a web service:

-(void)uploadPhoto:(UIImage*)image :(NSString*)fileName
{

NSData *img = UIImageJPEGRepresentation(image, 0.9f);

NSString *urlString = @"http://serverURL.com";

NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[NSData dataWithData:img]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);

}

This is working fine and I can get the photo from the server in PHP.

My question is, is it possible to send additional data to the server with the form-data? I need to send more details about the photo like the username and some other application related data.

I am wondering how I can append some additional data to the request. I've tried to append regular POST data to it but it's not working. I am assuming because the data type.

I am confused how this works and if there is a way for me to append more data to the request I am sending to the server.

Any help on this would be great.

Thanks!

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Sequenzia
  • 2,333
  • 9
  • 40
  • 59
  • You can use be this library that will work on any kind of web service: github.com/mineshpurohit/ServiceCallingUtility – MinuMaster Nov 15 '14 at 06:53

2 Answers2

1

To upload image on server one of the best way is to use ASIFormDataRequest.

You can send multiple values for the same parameter using the alternative add API:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

For a working code Uploading file to server using ASIFormDataRequest. Some part of how to use it...

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setUseKeychainPersistence:YES];
//if you have your site secured by .htaccess

//[request setUsername:@"login"];
//[request setPassword:@"password"];

NSString *fileName = [NSString stringWithFormat:@"ipodfile%@.jpg",self.fileID];
[request addPostValue:fileName forKey:@"name"];

To have better idea for Sending a form POST with ASIFormDataRequest.

Community
  • 1
  • 1
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
  • It looks like ASIHTTPResquest is no longer being actively developed ([link](http://allseeing-i.com/ASIHTTPRequest/)), [AFNetworking](https://github.com/AFNetworking/AFNetworking) seems to be the most popular replacement. – Mike D Mar 08 '13 at 20:34
0

Just repeat the following lines for each variable in the post:

[postbody appendData:[@"Content-Disposition: form-data; name=\"userfile\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[NSData dataWithData:img]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

Obviously you need to update the name value in the first line, the content type in the 2nd, and the actual data in the 3rd line.

rmaddy
  • 314,917
  • 42
  • 532
  • 579