Small but quick snippet here.
Sending multi-part form with image data:
NSString *imagePath = NSString *imagePath = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"junny.png"]; UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; NSData * imageData = UIImageJPEGRepresentation(image, 0.75) ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:http://example.com/form.php]]; [request setPostValue:@"I like brains!" forKey:@"title"]; [request setPostValue:[NSString stringWithFormat:@"%@", [NSDate date]] forKey:@"session"];l [request setPostValue:@"image.png" forKey:@"Filename"]; [request setData:imageData forKey:@"Filedata"]; // Get image, convert to JPG, get data [request setDelegate:self]; [request startAsynchronous];
The ASIFormDataRequest, is the BEST class for handling POST/GET/Multi-Part http request. It has a lot of classes, and its a little bulkier because it does what it does amazingly well. I’ve tried a few other implementations and this thing saved my life at 4am when I was about to start crying on this project
Tags: Uncategorized
I am working on one ipad application and some problem.I want to When i click on Image then after Link open where the Image come from so if you have any code then post…
That doesn’t sound like an HTTP-POST request problem, sounds like like you just want to send a user to a webpage.
I would google, ‘UIWebView’
This is a noob question but this is one of the better snippets I’ve stumbled on. How do you know on the server that this post is from a valid user.
Do login -> get response with some session value -> post image with session -> validate session against server db for that user/session/whatever other limits are set on that session?
I’ve found lots of tutorials for the initial login and then several for the form data but I’m looking to put it all together and I keep getting lost
Hi Tom,
What you normally do, is have the user login in someway then send them back a session-id when they successfully login. One of the fields you can send to the server, is that session-id which then checks it against currently active sessions.
Another more naive approach, at least to get you started is to just send the username and password with the image.