1

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.

5
  • You did correctly convert the image to data but you are trying to view the binary as a string. That output is part of the PNG header and it stops printing because a null character occurred. To dump data you need to use hex. (NSLog an NSData object will do a hex dump) Commented Mar 6, 2012 at 15:22
  • do you know how to show the image using php? Commented Mar 6, 2012 at 15:24
  • -bytes is definitely not deprecated. It's the class's primitive! What led you to believe it was? Commented Mar 6, 2012 at 15:32
  • If doing stuff manually is becoming too stressful try ASIHTTPRequest: github.com/pokeb/asi-http-request Commented Mar 6, 2012 at 18:39
  • But I found out that ASIHTTPRequest can't use on iOS 5….. is it? Commented Mar 7, 2012 at 9:55

1 Answer 1

2

Finally I figured out the solution by myself.

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://XXXX"]];
NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"uploadedfile" fileName:dateTime mimeType:@"images/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];
[operation setCompletionBlock:^{
    NSLog(@"response string: %@", operation.responseString); //Lets us know the result including failures
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

I used the AFNetworking instead of ASIHttpRequest.

On the php side, my code:

<?php

$filename="uploaded";
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

And thank you guys who helped me.

PS:Thanks a lot for this person who actually helped me on this: http://6foot3foot.com/developer-journal/afnetworking-php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.