Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 05-08-2008, 12:27 AM   #1 (permalink)
ddavtian
New Member
 
Join Date: Apr 2008
Posts: 10
Default Need Help Posting an image from iPhone to php page via POST

I am hoping someone can help with this issue ... I grab the image from iPhone's photo library and I am trying to post to a php page using POST, the data gets there but it seems to be corrupted, or at least we can't open it, it sends a png file..Can someone shine some light on this? Here's the code ..

-(void) grabImageUIImage *)theImage {

//creating the url request
NSURL *url = [NSURL URLWithString:@"http://example.com/app/test/"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:url];

//adding header information
[postRequest setHTTPMethod:@"POST"];

NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSData *imageData = UIImagePNGRepresentation(theImage);

if (imageData == nil) {
NSLog(@"Bad Image");
}

//setting up the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"sender_email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"ddavtian@maccius.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"recipient_emails\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"noc@maccius.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"postcard_message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"hello this is a test"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"ext\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"png"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"postcard\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// [postBody appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSData alloc] initWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBodyostBody];

NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequestostRequest delegate:self];

if (!connectionResponse) {
NSLog(@"Failed to submit request");
} else {
NSLog(@"Request submitted");
}
}
ddavtian is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,557
Threads: 89,127
Posts: 380,254
Top Poster: BrianSlick (7,103)
Welcome to our newest member, wTaylorDaviesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.