2

I want to upload data, images, txt files, pdf documents , etc. Im using MKNetworkKit to connect cleanly. With examples of this framework i try do it. I add to my proyect a class extends of MKNetworkEngine with a method for upload from file:

#import "MKNetworkOperation.h"
#import "MKNetworkEngine.h"

@interface Uploader : MKNetworkEngine
-(MKNetworkOperation*) uploadFromFile:(NSString*) file 
                              onError:(MKNKErrorBlock) errorBlock; 
@end



#import "Uploader.h"

@implementation Uploader


-(MKNetworkOperation*) uploadFromFile:(NSString*) file 
                              onError:(MKNKErrorBlock) errorBlock 
{

    MKNetworkOperation *op = [self operationWithPath:@"IP:PORT/PROYECT/" 
                                              params:nil
                                          httpMethod:@"POST"];

    [op addFile:file forKey:@"upload"];
    [op setFreezable:YES];

    [op onCompletion:^(MKNetworkOperation* completedOperation) 
     {
         NSString *xmlString = [completedOperation responseString];
         NSUInteger start = [xmlString rangeOfString:@"<mediaurl>"].location;
          if(start == NSNotFound) 
          {
          NSLog(@"%@", xmlString);
          errorBlock(nil);
          return;
          }
          xmlString = [xmlString substringFromIndex:start + @"<mediaurl>".length];
          NSUInteger end = [xmlString rangeOfString:@"</mediaurl>"].location;
          xmlString = [xmlString substringToIndex:end];
     }
             onError:^(NSError* error) 
     {
         errorBlock(error);
     }];

    [self enqueueOperation:op];
    return op;

}
@end

I have added i my interface a button for upload file, and in my controller i,ve created a method with this content:

-(IBAction)uploadImageTapped:(id)sender 
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString *cachesDirectory = [paths objectAtIndex:0];
    NSString *uploadPath = [NSString stringWithFormat:@"%@/Imagen.jpg",cachesDirectory];
    //NSLog(@"%@",uploadPath);

    self.uploadOperation = [ApplicationDelegate.sampleUploader uploadFromFile:uploadPath  onError:^(NSError* error) 
                            {
                                [UIAlertView showWithError:error];
                            }];
  }

Finally I,ve added a Uploader object in my delegate of app:

@property (strong, nonatomic) Uploader *sampleUploader;

and

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;

    //Uploader
    self.sampleUploader = [[Uploader alloc] initWithHostName:@"http://IP:PORT"
                                          customHeaderFields:nil];

    return YES;
}

My problem is that i want to can upload files, my localhost is not reachable and i dont know why....

Any ideas???

Davidin073
  • 991
  • 4
  • 12
  • 25

2 Answers2

1

RichS is right. You shouldn't use http (protocol) in the hostname.

Mugunth
  • 14,461
  • 15
  • 66
  • 94
0

[I've stumbled across your question whilst looking for some help myself with MKNetworkingKit.]

Quickly reading through you code, I wonder if you should remove the 'http://' from your initWithHostName. I'm sure the kit add 'http://' somewhere within its own init.

RichS
  • 3,097
  • 30
  • 26
  • Hi, I've got my instance working now. It is pretty much the same as your code. But, I've noticed you've got 'operationWithPath', but the parameter looks like a URI. You may need to use 'operationWithString' if you are passing in a full URI. Not sure if this helps. PS: I should be able to extract my code from my project and post if you still can't get it working. – RichS Apr 05 '12 at 12:32