Thursday, June 24, 2010

sending image and text to server

NSString *uTitle=text.text;
NSString *uMessage=myTextView.text;
NSData *imageData = UIImageJPEGRepresentation(imageView.image, .9);
NSString *post =[[NSString alloc] initWithFormat:@"&title=%@&message=%@&imagedetal=%@",uTitle,uMessage,imageData];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

///////////////////////////


// setting up the URL to post to
NSString *urlString = @"http:/_post.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];


[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];


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




NSMutableData *body = [NSMutableData data];
//[body appendData:[[NSString stringWithFormat:@"%@", post] dataUsingEncoding:NSUTF8StringEncoding]];

// title
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:uTitle] dataUsingEncoding:NSUTF8StringEncoding]]; // title
// message
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:uMessage] dataUsingEncoding:NSUTF8StringEncoding]]; // title



/// image

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"action=upload&"] dataUsingEncoding:NSUTF8StringEncoding]];
///





[body appendData:[NSData dataWithData:imageData]];







[request setHTTPBody:body];
NSString *msgLength = [NSString stringWithFormat:@"%d", [body length]];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);
[self alert];

No comments:

FEEDJIT Live