I need to send image to my server using post request. I have written the required PHP code for it. But how I have to send it from my device? I have seen ASIHTTPRequest framework, but it is out of date, and AFNetworking is complicating things. If I could I would want to send an image through GET request, but i just don't know how.
Asked
Active
Viewed 513 times
0
-
Check this out: http://stackoverflow.com/questions/7738704/sending-multipart-post-from-ios-and-reading-parameters-in-php-post In general, what you're trying to do is submitting a POST with a data block of type `multipart/form-data`. May this be your Google search term. – Seva Alekseyev Jun 14 '12 at 20:44
1 Answers
0
I created a subclass which allows you to create easy asynchronous POST requests and can be found here: https://github.com/MaxKDevelopment/MKNetwork.
To use it to upload an image, all you need to do is add the image data as a parameter to the request:
[MKNetwork sendAsynchronousRequest:[NSURL URLWithString:@"url to upload to"] params:[NSDictionary dictionaryWithObject:UIImagePNGRepresentation(imagedata) forKey:@"image"] decodeResponse:YES callback:^(id response, NSError *error) {
[someObject doSomethingWithResponse:response];
}];
Or, you could look into ASIHTTPRequest (ASIFormDataRequest): http://allseeing-i.com/ASIHTTPRequest/
Then initialise a ASIFormDataRequest variable with a POST value set as the UIImage.
NSURL *url = [NSURL URLWithString: @"url to upload to];
ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addPostValue:UIImagePNGRepresentation(imageToUpload) forKey:@"image"];
[request setCompletionBlock:^{NSLog(@"completed");}];
[request setFailedBlock:^{NSLog(@"failed");}];
[request startAsynchronous];

max_
- 24,076
- 39
- 122
- 211
-
1As far as I know ASIHTTP is not supported any more and it is out of date? – Ilya Lapan Jun 14 '12 at 20:47
-
-1 for suggesting ASI, it's deprecated, and there are much better alternatives out there, such as RESTKit, or AFNetworking. – Richard J. Ross III Jun 14 '12 at 20:47
-
I have provided an alternative to ASIHTTPRequest too. Please can you give me a link to the article outlining the deprecation of ASI as I hadn't heard that before. – max_ Jun 14 '12 at 20:50
-
2ASIHTTP is still used by many applications. There is no reason to downvote an answer simply because it uses older technology. It's not like your viewing this website via technology that's decades old, oh wait.... – FreeAsInBeer Jun 14 '12 at 20:51
-
Is it possible to avoid all this POST request headache? Is it possible to simplify it, use GET for example. Because it don't really care is it right or wrong, I am really short on time, and I am doing just a demo. – Ilya Lapan Jun 14 '12 at 20:54
-
In my answer, I outlined an easy way to do so, with the code that you need to use. – max_ Jun 14 '12 at 20:55
-
It is giving me error The operation couldn’t be completed. (NSURLErrorDomain error -1012.) What might be the reason? – Ilya Lapan Jun 15 '12 at 18:48
-
The URL that you are passing to it is incorrect. Also, if this answer has helped you, please accept it. – max_ Jun 15 '12 at 19:00