Question

I want to upload an image from device to server. But its not uploading! Even my return success value is showing null. Success value should be either 1 or 0. My code is given here. Please tell me if i am doing any mistakes in my code. Thanks in advance for the help.

-(void)ImageUpload{

NSString *urlString = [NSString stringWithFormat:@"%@upload.php", APIheader];

NSString *postLength = [NSString stringWithFormat:@"%d", [imgDATA length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlString]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imgDATA];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"return string: %@",returnString);

    returnString = [returnString stringByReplacingOccurrencesOfString:@"(" withString:@"["];
    returnString = [returnString stringByReplacingOccurrencesOfString:@")" withString:@"]"];
    returnString = [returnString stringByReplacingOccurrencesOfString:@";" withString:@""];
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    NSArray *array = (NSArray *)[parser objectWithString:returnString error:nil];
    NSString *status = [NSString stringWithFormat:@"%@",[[array objectAtIndex:0]objectForKey:@"success"]];

    if ([status isEqualToString:@"1"]) {

        NSLog(@"Image Updated");
    }
    else{

        NSLog(@"status is: %@",status);
    }
}

[request release];

}
Was it helpful?

Solution

You can use the CoreGraphics' method UIImagePNGRepresentation(UIImage *image), which returns NSData and save it. and if you want to convert it into again UIImage create it using [UIimage imageWithData:(NSData *data)] method. Some Questions related to this post image to server in iphone and How to convert image into binary format in iOS? as giving the same answer 3 time.

- (void)sendImageToServer {
       UIImage *yourImage= [UIImage imageNamed:@"image.png"];
       NSData *imageData = UIImagePNGRepresentation(yourImage);
       NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

       // Init the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:imageData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // response data of the request
       }
       [request release];
 }

OTHER TIPS

Use this code to upload any image to the server. This works for me. :)

   UIImage *yourImage= [UIImage imageNamed:@"image.png"];
   NSData *imageData = UIImagePNGRepresentation(yourImage);
   NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

   // Init the URLRequest
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   [request setHTTPMethod:@"POST"];
   [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
   [request setHTTPBody:imageData];

   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   if (connection) {
      // response data of the request
   }

Try this

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

Note

"BoundaryConstant" is basically the variable "boundary" which is essentially a random (NSString *), "FileParamConstant" is basically your "filename.jpg"

You can also do by importing ASIFormDataRequest calss,Here is the link which tells Why to use this,after downloading classes just do like this,

uploading image to server using ASIFormDataReqest

Hope it will Helps you.....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top