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

View Single Post
Old 06-07-2009, 08:29 PM   #1 (permalink)
michelle
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
 

» Advertisements
» Stats
Members: 158,847
Threads: 89,213
Posts: 380,661
Top Poster: BrianSlick (7,129)
Welcome to our newest member, Leroy2027
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:38 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.