Showing posts with label Disallowed Key Characters. Show all posts
Showing posts with label Disallowed Key Characters. Show all posts

Thursday, 18 July 2013

Upload image using AFNetworking in iOS with CodeIginiter script | Disallowed Key Characters

When I was calling my service method which is using codeiginiter upload script from server, it returns error "Disallowed Key Characters". Actually in my case send (null) to the service. So, I add two special characters ( ) in _clean_input_keys function of codeiginiter. Let say, BASE_URL = http://localhost:8888 SERVICE_PATH = /test_upload/upload.php YOUR_FILE_NAME = test.png FORM_POST_NAME_VALUE = profile_image
This form post name value should be same on both client and server side. It will identify your post value. (i.e profile_image will accept your post body)

iPhone Code :
NSData *imageData = UIImagePNGRepresentation(YOUR_IMAGE_VIEW);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"BASE_URL"]];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"SERVICE_PATH" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData:imageData name:@"FORM_POST_NAME_VALUE" fileName:@"YOUR_FILE_NAME" mimeType:@"image/png"];
 }];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSString *result = [operation responseString];
                  NSLog(@"response: [%@]",result);
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
 if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        return;
 }
    NSLog(@"error: %@", [operation error]);
 }];
      [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
      NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
     //float width = totalBytesWritten / totalBytesExpectedToWrite;
 }];
    [operation start];
In my case $str having (null), so I add this 2 special characters. If you are having another special characters, please add them in below function as regular expression.
CodeIginiter Script:
function _clean_input_keys($str){
if ( ! preg_match("/^[a-z0-9:_\(\)\/-]+$/i", $str)){
        exit('Disallowed Key Characters.');
    }
   return $str;
}
Cheers !!