views:

31

answers:

2

Hey, can anyone post a sample code demonstrating how can images (jpeg, png) be posted on a local web service.

Sayeed

A: 

First google hit would probably help you.

bua
A: 

Here is a code snippet -

- (void)useImage:(UIImage*)theImage
{
    /*
     turning the image into a NSData object
     getting the image back out of the UIImageView
     setting the quality to 90
     */

    NSData *imageData = UIImageJPEGRepresentation(theImage, 90);
    // setting up the URL to post to


    NSString *urlString=[SiteUrl    stringByAppendingString:@"iphone_upload_photo.php?type=colleague&token="];
    urlString=[urlString stringByAppendingString:PublicToken];





    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same

     */
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.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];





}

Edit: Explaination of the code -

What we are doing here is, we are just submitting an image to a server side page exactly as we do in HTML page Suppose we have an html page like this -

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

we can receive this file in PHP code as in here -

$target_path = "uploads/";

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

// $_FILES['userfile']['name'] will be "ipodfile.jpg"

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

So what we are exactly doing in objective code is we are just creating an POST request and setting http header for this post request that we are sending a file and the filefield name is "userfile" and the exact file name is "ipodfile.jpg"

you can use the php code as is to test this thing.

Hope it will clear the confusion.

Saurabh
hey saurabh, what is this line:[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
Bogus Boy
In this line we are setting Http header "Content-Disposition" setting the input file field name and the name of the file(that you will receive on server). Let me know if you need more details on this. I am available for 4-5 hours.
Saurabh
yes boss, I am clueless as to how it is achieved. How should this NSData we are passing be received at the other end(web service) and decoded so as to get the original image. Earlier, I was able to convert small images (upto 4kb) into nsstring base64 encoding and send them to the webservice and unpack it there to get the images. But now the requirement is to do it for images of all sizes. Could you walk me through the code? For example- I am having an image named "van gogh.png", now what should I be doing exactly?
Bogus Boy
I have edited my answer please check..
Saurabh