简体   繁体   中英

iOS SDK Post Photo to web service with additional data

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!

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM