I've been working on uploading an UIImage to an webserver. The problem is, that the image never appears on the Server. A normal POST from an HTML file works. I think it's an issue in my objective-c code.
Here is my objective-c code:
NSData *imageData = UIImagePNGRepresentation(delegate.dataBean.image); NSString *urlString = [NSString stringWithFormat:@"%@test.php", delegate.dataBean.hosterURL]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog([NSString stringWithFormat:@"Image Return String: %@", returnString]);
Here is my PHP code:
<?php $uploaddir = 'uploads/'; $file = basename($_FILES['uploadedfile']['name']); $uploadfile = $uploaddir . $file; echo "file=".$file; //is empty, but shouldn't if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) { echo $file; } else { echo "error"; } ?>
basename( $_FILES['photo']['name']); //This gets all the other information from the form $pic = ($_FILES['photo']['name']); //Writes the photo to the server if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".
php file and show you layout of your webcam with "Take Snapshot" button, when you will click on that button js library will capture image on base64 string. after that when click on submit button then picture will store in directory. Read Also: PHP - How to create zip file and download using ZipArchive ?
Now i've solved it by myself =)
I forgot the backslashes in the body.
Here is the right body:
NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With