1

i am uploading the Image,which is taken from the camera or gallery.

i am successfully getting the image and when i uploading it i am getting error in simulator User_file=(null).

My Updating code is

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{



    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

    NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);

    NSString *convertimage=[[NSString alloc]initWithData:dataItems encoding:NSUTF8StringEncoding];



    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];




    NSURL * url = [NSURL URLWithString:@"http:my url"];
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];




//        
//    NSString *boundary = @"---------------------------14737809831466499882746641449";
//    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
//    [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//    
//    NSMutableData *body = [NSMutableData data];
//    
//    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"%d\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    [body appendData:[NSData dataWithData:dataItems]];
//    
//    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    [urlRequest setHTTPBody:body];




    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    // getting an NSString
    NSString *UserID = [prefs stringForKey:@"sfffehr_id"];

    NSLog(@"%@",UserID);


    NSString *params=[[NSString alloc]initWithFormat:@"sfffehr_id =%@ & user_file=%@",UserID,convertimage];

    NSLog(@"%@",params);
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];


    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                           NSLog(@"Response:%@ %@\n", response, error);
                                                           if(error == nil)
                                                           {
                                                               NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                               NSLog(@"Data = %@",text);
                                                           }

                                                       }];
    [dataTask resume];

my response is

 fdg[1816:949041]  user_file=(null)
 fdg[1816:949041] Logo Changed
 fdg[1816:949041] Response:<NSHTTPURLResponse: 0x136a2bcb0> { URL: url } { status code: 200, headers {

fdg[1816:949041] Data = {"response":"yes","success":"User profile picture updated successfully"}

When i check in my server i did't find my saved image.

How to save the image in my server, please help me .

I tried many methods but it can't help me.

Thank you.

NAVEEN KUMAR
  • 669
  • 6
  • 25

1 Answers1

3

I have made some corrections on your code. Try this now:

UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);
NSString *convertimage = [[NSString alloc] initWithData:dataItems encoding:NSUTF8StringEncoding];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL * url = [NSURL URLWithString:@"http://469.5350.747.4557/~lcall/index.php/webservices/update_profile_picture"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *UserID = [prefs stringForKey:@"user_id"];

//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:UserID forKey:@"user_id"];
[parameters setValue:convertimage forKey:@"user_file"];

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

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"image.png\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataItems]];

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

// getting an NSString
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Response:%@ %@\n", response, error);
    if(error == nil){
        NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
        NSLog(@"Data = %@",text);
    }
}];
[dataTask resume];

Note: I have make necessary corrections in your code & Its Error free. But i have not tested it. Please check.

I hope it will work for you.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70