I\'m writing an iPhone app that takes a photo and then uploads it to a server. How do I upload a photo to a server with Cocoa? I suppose I use NSUrl somewhere.
Tha
- (void) uploadImage :(NSString *) strRequest
{
if([appdel checkNetwork]==TRUE)
{
NSString *urlString =[NSString stringWithFormat:@"Enter Url........."];
NSLog(@"Upload %@",urlString);
// setting up the request object now
isUploadImage=TRUE;
totalsize=[[strRequest dataUsingEncoding:NSUTF8StringEncoding]length];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"_1_19330907_1317415362628"];
NSString *contentType = [NSString stringWithFormat:@"multipart/mixed; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" forHTTPHeaderField:@"Accept"];
[request setValue:@"2667" forHTTPHeaderField:@"Content-Length"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[NSData dataWithData:imageData]];
[body appendData:[strRequest dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
webData = [[NSMutableData data] retain];
else
NSLog(@"No Connection");
}
}