-1

I am using follwing Code as i am new to xcode please tell me how to send image with the data. The data is posting perfectly to the server. Just add the code so that i can send the image to the Server:

- (IBAction)senddatatophp:(id)sender {

    NSLog(@"Working insert button");

    NSString *did = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];
    NSString *deviceTokenStr = [[[[did description]
                                  stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                 stringByReplacingOccurrencesOfString: @">" withString: @""]
                                stringByReplacingOccurrencesOfString: @" " withString: @""];

    //NSLog(@"%@", deviceTokenStr);

    NSData *getimgdata1 = UIImageJPEGRepresentation(_frontimg.image, 1);
    NSLog(@"%@", getimgdata1);


    NSString *name=@"Admin";
    NSString *lastname=@"panel";

    NSString *tag       =   @"watchlist";
    NSString *gcmid     =   deviceTokenStr;
    NSString *brand     =   textbox1.text;
    NSString *model     =   textbox2.text;
    NSString *year      =   textbox3.text;
    NSString *condition =   textbox4.text;
    NSString *box       =   textbox5.text;
    NSString *warranty  =   textbox6.text;
    //NSString *front_img =   _path1;
    //NSString *back_img  =   _path2;
    //NSString *side_img  =   _path3;

    NSString *sendData = @"name=";
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", name]];
    sendData = [sendData stringByAppendingString:@"&lastname="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", lastname]];



    sendData = [sendData stringByAppendingString:@"&tag="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", tag]];
    sendData = [sendData stringByAppendingString:@"&gcmid="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", gcmid]];
    sendData = [sendData stringByAppendingString:@"&brand="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", brand]];
    sendData = [sendData stringByAppendingString:@"&model="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", model]];
    sendData = [sendData stringByAppendingString:@"&condition="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", condition]];
    sendData = [sendData stringByAppendingString:@"&year="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", year]];
    sendData = [sendData stringByAppendingString:@"&box="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", box]];
    sendData = [sendData stringByAppendingString:@"&warranty="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", warranty]];
    /*
    sendData = [sendData stringByAppendingString:@"&front_img="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", front_img]];
    sendData = [sendData stringByAppendingString:@"&back_img="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", back_img]];
    sendData = [sendData stringByAppendingString:@"&side_img="];
    sendData = [sendData stringByAppendingString:[NSString stringWithFormat:@"%@", side_img]];
    */


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.onlinestoresolution.net/demo/buymywatch/buymywatch/index.php"]];

    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

    //Here you send your data
    [request setHTTPBody:[sendData dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPMethod:@"POST"];
    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *stringReply = (NSString *)[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    _SendWatchId = stringReply;

    if (error)
    {
        NSLog(@"Error: %@", stringReply);

    }
    else
    {
        //The response is in data
        NSLog(@"Success: %@", stringReply);
    }


} 
Popeye
  • 11,839
  • 9
  • 58
  • 91
Sandeep Kumar
  • 619
  • 6
  • 18

3 Answers3

1

use this for sending image file. write down this code, once you have posted your text data on server.

 UIImage *img=self.imgView.image;
NSData *imageData = UIImagePNGRepresentation(img);
NSString *urlString = [NSString stringWithFormat:@"%@upload.php",@"http://localhost:8888/ImageUploadiOS/"];

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=\"uploadedfile\"; filename=\"test%d.png\"\r\n",self.fileNum] 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(@"Image Return String: %@", returnString);
self.fileNum++;
UIAlertView *alertUpload=[[UIAlertView alloc] initWithTitle:@"DemoApp" message:@"Image Uploaded On Server" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertUpload show];

php code for stroing image data on server is below.

    <?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";
}
?>

don't forget to change url of request. for sending multiple image code is

        //image1
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile1\"; filename=\"testing1.png\"\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]];
//end here image1



//image2
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile2\"; filename=\"testing2.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData2]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//end here image2

Php code for handling muliple images at once is

      <?php
$uploaddir = 'uploads/';
$file1 = basename($_FILES['uploadedfile1']['name']);
$uploadfile = $uploaddir . $file1;

echo "file=".$file1; //is empty, but shouldn't

if (move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $uploadfile))
{
    //echo $file;
    //echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
    echo 'file1 moved';
}
else
{
    echo "error";
}



$file2 = basename($_FILES['uploadedfile2']['name']);
$uploadfile = $uploaddir . $file2;

echo "file=".$file2; //is empty, but shouldn't

if (move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $uploadfile))
{
    //echo $file;
    //echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
    echo 'file2 moved';
}
else
{
    echo "error";
}

?>

Sheshnath
  • 3,293
  • 1
  • 32
  • 60
0

Not that any of this has any sense but if you wish to send both the string data and the image data you need to append the data together. To do so it is easiest to use a mutable version of the NSData:

NSData *input1;
NSData *input2;
NSMutableData *output;

output = [[NSMutableData alloc] initWithData:input1];
[output appendData:input2];

Then input1 is your string data and input2 your image data (or vice versa) and your output is what you use in the body.

And the first thing you might want to look in is to send the request asynchronously since you are dealing with the large amount of data.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
0
// add image data
NSData *getimgdata1 = UIImageJPEGRepresentation(_frontimg.image, 1);
if (getimgdata1) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:getimgdata1];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

refer to this NSData and Uploading Images via POST in iOS

Community
  • 1
  • 1
Sport
  • 8,570
  • 6
  • 46
  • 65