0

I am trying to upload an array of images the user selects . They are base64 encoded . I am getting a null response from the server . Here is my code below.

for (PHAsset *asset in assets) {

[manager requestImageForAsset:asset  targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:self.requestOptions resultHandler:^void(UIImage *image, NSDictionary *info) {


   NSString *encodeStr = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];      

  [images addObject:encodeStr];

   }];
}

I am getting images from delegate using QBImagePickerController . I have a NSMutableDictionary as the body content for NSMutableURLRequest .

[uploadObject setValue:@"32" forKey:@"userid"];        
[uploadObject setValue:TFname.text forKey:@"Name"];        
[uploadObject setObject:images forKey:@"SelectedImages"];        


JSONData = [NSJSONSerialization dataWithJSONObject:uploadObject options:NSJSONWritingPrettyPrinted error:nil];

Here is the NSMutableURLRequest

request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:@"JSON" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:JSONData];

I am getting a server response code as 500 and a null response from the server . I have tried both NSURLSessionDataTask and NSURLSessionUploadTask. Any help would do good !

user5553647
  • 169
  • 2
  • 12

2 Answers2

1

There's no way to determine whether this code is correct or not without seeing the server-side code. It looks like you're sending the following to the server:

{
  "userid" : "32",
  "Name": "someFilename",
  "SelectedImages": [
      "base64-encoded-data-of-first-image",
      "base64-encoded-data-of-second-image",
      ...
  ]
}

If your server understands how to parse a JSON request body and take fields with those names, then it should work just fine. HOWEVER, it seems unlikely that the format is correct, as you're passing in multiple images with what appears to be single filename.

If you are using existing server code, you need to find out what format it expects the data to be in. If you're writing your own server code, you should probably structure the data more like this:

{
  "userid" : "32",
  "SelectedImages": [
     {
        "name": "firstFilename",
        "data": "base64-encoded-data-of-first-image"
     },
     {
        "name": "secondFilename",
        "data": "base64-encoded-data-of-second-image"
     },
      ...
  ]
}
dgatwood
  • 9,519
  • 1
  • 24
  • 48
0

Finally I Found the issue sometime ago .

The problem was with the server side .

user5553647
  • 169
  • 2
  • 12