Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-07-2009, 08:29 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default Upload image to Java Servlet

I finally got this to work, the key was to make sure you load the image first.

You can see a storyboard of the process here.



- (IBAction)uploadImage {
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/


UIDevice * dev = [UIDevice currentDevice];
NSString *uniqueId = dev.uniqueIdentifier;
NSString *name = @"photo1";
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
//NSData * imageData = UIImagePNGRepresentation(image);
// NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
NSString *urlString = [@"http://172.16.1.37:8080/tree-selection-tutorial/httpproxy?" stringByAppendingString:@"uid="];
urlString = [urlString stringByAppendingString:uniqueId];
urlString = [urlString stringByAppendingString:@"&name="];
urlString = [urlString stringByAppendingString:name];
urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];



//add data field and file data
[postBody appendData:[NSData dataWithData:imageData]];

// ---------

[request setHTTPBodyostBody];

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

- (void)imagePickerControllerUIImagePickerControll er *)picker didFinishPickingImageUIImage *)img editingInfoNSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

// need to show the upload image button now
upload.hidden = NO;
}

@end
michelle is offline   Reply With Quote
Old 06-10-2009, 09:00 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by michelle View Post
I finally got this to work, the key was to make sure you load the image first.

You can see a storyboard of the process here.



- (IBAction)uploadImage {
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/


UIDevice * dev = [UIDevice currentDevice];
NSString *uniqueId = dev.uniqueIdentifier;
NSString *name = @"photo1";
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
//NSData * imageData = UIImagePNGRepresentation(image);
// NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
NSString *urlString = [@"http://172.16.1.37:8080/tree-selection-tutorial/httpproxy?" stringByAppendingString:@"uid="];
urlString = [urlString stringByAppendingString:uniqueId];
urlString = [urlString stringByAppendingString:@"&name="];
urlString = [urlString stringByAppendingString:name];
urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];



//add data field and file data
[postBody appendData:[NSData dataWithData:imageData]];

// ---------

[request setHTTPBodyostBody];

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

- (void)imagePickerControllerUIImagePickerControll er *)picker didFinishPickingImageUIImage *)img editingInfoNSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

// need to show the upload image button now
upload.hidden = NO;
}

@end


Sorry , bad link

http://web.me.com/cannonwc/Site_6/StoryBoard.html
michelle is offline   Reply With Quote
Old 07-17-2009, 06:51 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 9
Default

If I were receiving this info with a PHP form - what would the file name be for the body ??

i.e. $_FILES['filename'] ???

Thanks for your code!! It seems to work great, I just don't know how to get the image data.
funnylookinhat is offline   Reply With Quote
Old 10-22-2009, 10:28 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default Hi, Could you post the server side to this solution?

Hi, thanks for the code example. I was hoping you could pull the relevant code from the server side to show the complete end to end workings.
thanks in advance!
Brian

Quote:
Originally Posted by michelle View Post
I finally got this to work, the key was to make sure you load the image first.

You can see a storyboard of the process here.



- (IBAction)uploadImage {
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/


UIDevice * dev = [UIDevice currentDevice];
NSString *uniqueId = dev.uniqueIdentifier;
NSString *name = @"photo1";
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
//NSData * imageData = UIImagePNGRepresentation(image);
// NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
NSString *urlString = [@"http://172.16.1.37:8080/tree-selection-tutorial/httpproxy?" stringByAppendingString:@"uid="];
urlString = [urlString stringByAppendingString:uniqueId];
urlString = [urlString stringByAppendingString:@"&name="];
urlString = [urlString stringByAppendingString:name];
urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];



//add data field and file data
[postBody appendData:[NSData dataWithData:imageData]];

// ---------

[request setHTTPBodyostBody];

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

- (void)imagePickerControllerUIImagePickerControll er *)picker didFinishPickingImageUIImage *)img editingInfoNSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

// need to show the upload image button now
upload.hidden = NO;
}

@end
dealyb is offline   Reply With Quote
Old 10-22-2009, 08:04 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default Thank You!

Thank You!
Quote:
Originally Posted by dealyb View Post
Hi, thanks for the code example. it works for me!
really appreciate it!

Brian
dealyb is offline   Reply With Quote
Old 12-18-2009, 12:22 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 1
Default

Quote:
Originally Posted by funnylookinhat View Post
If I were receiving this info with a PHP form - what would the file name be for the body ??

i.e. $_FILES['filename'] ???

Thanks for your code!! It seems to work great, I just don't know how to get the image data.
Did you ever get an answer to this? I'm trying to do the same...thanks!!!
laserkitter is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 595
21 members and 574 guests
Andy8888, Arfan, armmzz, bignoggins, BrianSlick, ckgni, devangvyas, Duncan C, Hito_kun, kvu, learningtocode, linkmx, LiquidFire, MarcoAlmeida, Monstertaco, oceanlablight, soonw29, sudama, Vineesh
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,775
Threads: 89,203
Posts: 380,590
Top Poster: BrianSlick (7,129)
Welcome to our newest member, Andy8888
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:46 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0