Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

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)
Junior Member
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0
ddavtian
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)
Senior Member
 
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Rep Power: 0
bonehead
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)
Junior Member
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0
ddavtian
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)
Senior Member
 
Join Date: Aug 2008
Location: Germany, Munich
Posts: 215
Rep Power: 1
rhuettl is on a distinguished road
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)
Member
 
Join Date: Jul 2008
Posts: 75
Rep Power: 1
hijinks is on a distinguished road
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

» Advertisements


» Stats
Members: 8,229
Threads: 20,197
Posts: 90,213
Top Poster: RickMaddy (2,121)
Welcome to our newest member, jrsiqueira
Powered by vBadvanced CMPS v3.1.0

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