How can I upload some text info (a text string) and an image file over the same http post request to server. I got images upload by itself, but can't get text to work with it. Thanks!
Asked
Active
Viewed 5.1k times
10
-
Objective c, This is a similar question, but I need text. – sebi Jun 29 '12 at 12:44
-
http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone – sebi Jun 29 '12 at 12:44
-
Are you using Which web service "REST" "SOAP" or "JSON" which one you will use? – Ravi Kumar Karunanithi Jun 29 '12 at 12:49
-
I'm using php, $_POST['text']; maybe REST at some point – sebi Jun 29 '12 at 12:51
-
1"Objective c, This is a similar question, but I need text. " I didn't ask what LANGUAGE (it's OBVIOUSLY Objective-C), but which SERVICE. Google docs? Instagram? Dropbox? WHAT? – Jun 29 '12 at 13:04
-
here i answered http://stackoverflow.com/questions/8474063/how-to-upload-image-dynamically-on-server/15153502#15153502 – vinothsivanandam Mar 01 '13 at 09:01
4 Answers
19
Use this code to upload image and textLabel
NSData *imageData = UIImageJPEGRepresentation("yourImage",0.2); //change Image to NSData
if (imageData != nil)
{
filenames = [NSString stringWithFormat:@"TextLabel"]; //set name here
NSLog(@"%@", filenames);
NSString *urlString = @"http://xxxxxxx/yyyyy.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
NSLog(@"finish");
}
In php side use this code
$myparam = $_POST['userfile']; //getting image Here
$mytextLabel= $_POST['filenames'] //getting textLabe Here
echo $myparam;
echo $mytextLabel;
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['myfile']['name']);
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['myfile']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}

Adi Inbar
- 12,097
- 13
- 56
- 69

Ravi Kumar Karunanithi
- 2,151
- 2
- 19
- 41
-
I can't tell where the text label is being set here, let's say the text is "hello". Where should it go in this code? – sebi Jun 29 '12 at 13:07
-
Ok thanks got it, btw I didn't use any of the assetname, or characterset stuff, as I'm not sure what it does. – sebi Jun 29 '12 at 13:27
4
Just Add Following Method On Button On Click :
-(void) uploadImage
{
prodNam = txtProdName.text;
UIImage * img = [UIImage imageNamed:@"SRT2.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img,0.2); //change Image to NSData
if (imageData != nil)
{
NSString * filenames = [NSString stringWithFormat:@"TextLabel"];
NSLog(@"%@", filenames);
NSString *urlString = @"http://dev9.edisbest.com/upload_image.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"TestEdreamzIpad.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Response : %@",returnString);
if([returnString isEqualToString:@"Success ! The file has been uploaded"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
NSLog(@"Finish");
}
}
You can put any image at the place of "SRT2.jpg" of your xcode local hierarchy.
All the best...

Pushkraj Lanjekar
- 2,254
- 1
- 21
- 34
-
1Hey can you upload your PHP COde too? I tried your code but it always say "There was an error uploading the file, please try again!" And no data appear on my server... – Laurenz Glück Mar 17 '13 at 15:25
-
2@LaurenzGlück : Code For : upload_image.php "iphonetest" is Directory of server placed in root directory. Use and reply is it worthy to you ? – Pushkraj Lanjekar Apr 26 '13 at 13:00
-
2
Simply User SVHttp Request to upload files to server.
send imageFile and other stuffs in parameter :
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:imageData forKey:@"image"];
[data setObject:@"saveimage" forKey:@"action"];
[data setObject:userId forKey:@"userid"];
[data setObject:@"png" forKey:@"type"];
And make Request :
SVHTTPClient *request = [SVHTTPClient sharedClient];
[request setBasicAuthWithUsername:nil password:nil];
[request setSendParametersAsJSON:NO];
[request POST:[NSString stringWithFormat:@"http://bylyngo.com/getapi/?action=saveimage&userid=%@&type=png",userId]
parameters:data
progress:^(float progress) {
NSLog(@"Uploading (%.0f%%)", progress*100);
}];
You will get progress even .
:)

Sam_9991
- 21
- 1
- 4
1
My web services is this one and it's working fine!
<?php
$fp = fopen("./trace.txt", "w"); //creates a file to trace your data
fwrite($fp,"get \n");
fwrite($fp, print_r($_GET, true));
fwrite($fp,"POST \n");
fwrite($fp, print_r($_POST, true));//displays the POST
fwrite($fp,"FILES \n");
fwrite($fp,print_r($_FILES,true));//display the FILES
fclose($fp);
$myparam = $_POST["userfile"];
$mytextLabel = $_POST['filenames'];
echo $myparam;
echo $mytextLabel;
$uploadDir = 'uploads/'; //you must create this directory
$uploadDir = $uploadDir.basename($_FILES['myfile']['name']); //saves the picture inside that folder
$file = basename($_FILES['uploaded']['name']);
//$uploadedFile = $uploadDir.$file;
//move_uploaded_file($_FILES['uploaded']['tpm_name']);
if(move_uploaded_file($_FILES['myfile']['tpm_name'],$uploadDir)){
echo "the file ".basename($_FILES['myfile']['name'])." has been uploaded" ;
}else{
echo "error";
}
?>

Adjaxon Araújo
- 69
- 1
- 7