First I show my related code:
convert UIImage into NSData:
imageData = UIImagePNGRepresentation(myImage);
Then I wrote the NSMutableRequest:
NSString *urlString = @"http://136.206.46.10/~katie_xueke/test.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
Then I wrote the NSMutableData:
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSMutableData *body = [NSMutableData data];
//Image
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name =\"image\";filename=\"%@\"\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n",postLength]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
$$Question is: The NSData seems can't send to the server side and the body part which showed in the console window is like this:
Content-Disposition: form-data; name ="image";filename="2012:03:06 15:06:48"
Content-Type:application/octet-stream
Content-Length: 164692
‰PNG
How to change uiimage's nsdata into binary data in order to send to php??
PS: I tried the Uint8 method
UInt8 *rawData = [imageData bytes];
But it seems that iOS 5 has deprecated it.
On the php side:
$uploaddir = './upload/';
echo "recive a image";
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "/uploads/{$file}";
}
I copied it from some other place and I don't know how to show up my POST image on the webpage.
Someone can help me?
Many thanks.
NSLoganNSDataobject will do a hex dump)-bytesis definitely not deprecated. It's the class's primitive! What led you to believe it was?