Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2008, 11:27 PM   #1 (permalink)
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
Old 05-08-2008, 12:06 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Default Re: Need Help Posting an image from iPhone to php page via POST

If I had to guess, I'd say that your multipart boundaries aren't quite right, or else your server isn't set up correctly to accept multipart POSTs.

Have you tried the simpler test of just sending a single image's data in the body of a POST, rather than diving into multipart?
bonehead is offline   Reply With Quote
Old 05-08-2008, 10:23 AM   #3 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 10
Default Re: Need Help Posting an image from iPhone to php page via POST

I have not tried that but I will, here's the php file that will accept the data ...

if(!isset($_REQUEST['sender_email']) || !isset($_REQUEST['ext']) ){

echo "result=fail&errorMsg=invalid request";
exit;
} else {

$headers = 'From: hello@example.com' . "\r\n" .
'Reply-To: hello@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$email = $_REQUEST['sender_email'];

$msg = "";
foreach ($_REQUEST as $k => $v) {
$msg .= " $k = $v\n";
}



mail($email, "Microphone Check", $msg, $headers);
echo "result=success&email=" . $email;
}

if(isset($_REQUEST['postcard'])){


$ext = $_REQUEST['ext'];

// define the image
$file = $_REQUEST['postcard']; //base64_decode($_REQUEST['postcard']);

// define the file name
$filename = time();

// define destination path to image
$destPath = "../postcards/" . $filename . "." . $ext;

// save the image
try {

@file_put_contents($destPath, $file);

} catch (Exception $ex) {

echo "&result=failed&errorMsg=" . urlencode($ex);
}

}
ddavtian is offline   Reply With Quote
Old 09-07-2008, 06:43 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Germany, Munich
Posts: 230
Default

Hi,

we have implemented posting some data (a photo made by the camera too) via JSON-String to URL. At first our image was corrupt too. You nee to encode your NSData to a Base64 and decode it on the server sid (we are using C#.NET) - that worked in both directions for us with png, jpg,...

Cheers
Ralf
rhuettl is offline   Reply With Quote
Old 09-07-2008, 08:08 PM   #5 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 75
Default

I have a blog post about this

Post a UIImage to the web | Iphone Noob

Looks like your boundaries are bad
__________________
Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
hijinks is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Posting an app update fasttech iPhone SDK Development 2 07-11-2008 07:40 AM
UIWebView how to reload page every time Marco iPhone SDK Development 1 07-06-2008 12:38 AM
web page search nownot iPhone SDK Development 0 07-04-2008 12:30 PM
UIButton with an image ddavtian iPhone SDK Development 1 04-30-2008 02:53 PM
Pulling Data From A Web Page CoffeeKid iPhone SDK Development 8 04-17-2008 11:43 AM

Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Online Users: 452
28 members and 424 guests
alexy, asimrs, Boh, bugman, dany88, Dmitry, Erle, fnicola, gluse, greetings, hatembr, hg4072, joalta, Johanovski, melmoup, Meoz, myPhone, nikhil, ovidiu, refreshe, rocksing2010, sandman4sure, sanjeev, skunk, Sniark, thomaswguy, tychop, vikysaran
Most users ever online was 779, 05-11-2009 at 09:55 AM.
» Stats
Members: 24,095
Threads: 38,876
Posts: 170,544
Top Poster: smasher (2,563)
Welcome to our newest member, hg4072
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0