Hey, can anyone post a sample code demonstrating how can images (jpeg, png) be posted on a local web service.
Sayeed
Hey, can anyone post a sample code demonstrating how can images (jpeg, png) be posted on a local web service.
Sayeed
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.