Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

objective c - Upload Image issue in Xcode and ios

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);
    }


} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile"; filename="test%d.png"
",self.fileNum] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile1"; filename="testing1.png"
"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//end here image1



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

[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData2]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",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";
}

?>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...