I am working with web services using json parsing. I am able to get images from the web services, can someone help me how to post an image? How i can post an image to the web services?
Asked
Active
Viewed 6,603 times
4
-
Did you consider using `ASIHTTPRequest` ? – limon Dec 03 '12 at 08:47
-
@mstfbsnli I am new to iOS...So,please help me, how i can implement it through ASIHTTPRequest..Please.. – Kundan Dec 05 '12 at 06:26
-
Here is the [explanation](http://allseeing-i.com/ASIHTTPRequest/How-to-use) of usage – limon Dec 05 '12 at 07:54
4 Answers
4
it will be something along the lines of this...
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:@"you url"];
[mutableRequest addValue:@"image/jpeg" forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)]];
[mutableRequest setHTTPBody:body];
NSURLResponse *response;
NSError *error;
(void) [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:&response error:&error];
Thanks

Fogmeister
- 76,236
- 42
- 207
- 306
-
-
Not sure what you mean which method? It depends entirely on how you have written your program. At some point you will have a method called "uploadImage" or something. Well this is how you upload that image. – Fogmeister Dec 03 '12 at 07:54
-
tnx.. I asked you such question because i am new to iOS.so,dont have so much idea abt it.. – Kundan Dec 03 '12 at 07:56
2
Check below good tutorial.In that tutorial you able to find the ios side code as well server side php code too. http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

Susim Samanta
- 1,605
- 1
- 14
- 30
-
tnx..but it is necessary to know server side..because i have only the url for post..no,server side info.. – Kundan Dec 03 '12 at 07:53
-
Just ask server side developer what's he have implemented also ask him to give a html form to validate his code. It may be something wrong in his side and that's may cause your nightmare:) – Susim Samanta Dec 03 '12 at 07:55
-
the link you have mentioned has described it nicely but post image not working there..you have some another link or code..please help.. – Kundan Dec 03 '12 at 08:29
-
2
Find here serverside (PHP) coding for image Upload with random name. also it will give the image link as response.
//Create a folder named images in your server where you want to upload the image.
// And Create a PHP file and use below code .
<?php
$uploaddir = 'images/';
$ran = rand () ;
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "www.host.com/.../images/{$uploadfile}";
}
?>
And Here is iOS code
- (IBAction)uploadClicked:(id)sender
{
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
// setting up the URL to post to
NSString *urlString = @"your URL link";
// 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
as my output from wireshark on a valid html post
*/
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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",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];
NSLog(returnString);
}

Siba Prasad Hota
- 4,779
- 1
- 20
- 40
-
I am new to iOS.so, is it possible for you to elaborate a little bit how to do this?? – Kundan Dec 04 '12 at 08:05
-
See my edited answer and let me know where you are facing issue or which line you cant understand. – Siba Prasad Hota Dec 04 '12 at 09:40
-
In my project my server side is not php its python.so,what the changes have to made?? – Kundan Dec 04 '12 at 10:05
-
-
@SibaPrasadHotaCan you please tell me if i need to send chunks of images on php web server at one time. so what should be the good way to do it. I have reduced image data size upto 100 kb.Thanks – Anand Gautam Dec 10 '13 at 10:40
-
@AnandGautam I already did that by using the same procedure in a for loop But, i dont know whether that is the right way. – Siba Prasad Hota Dec 10 '13 at 15:19
-
@SibaPrasadHotaCan you see this link and give me some suggestion - http://stackoverflow.com/questions/20513116/how-to-upload-image-file-from-document-directory-to-php-server-in-iphone/20513229?noredirect=1#comment30666756_20513229 – Anand Gautam Dec 11 '13 at 08:07
-
@AnandGautam Check my answer in you link, i tried that and worked. – Siba Prasad Hota Dec 11 '13 at 11:06
0
The best way to use ASIHttpFormData Click here to know how to use

SachinVsSachin
- 6,401
- 3
- 33
- 39
-
for New Projects its no longer working http://allseeing-i.com/ASIHTTPRequest/How-to-use – Siba Prasad Hota Dec 04 '12 at 05:47