0

All, I am using RestKit for iOS to upload a JPEG file to my Java web service. I referred to this

tutorial for developing file upload web service and it works perfectly fine when i use it through my web browser.

However, when i try to upload a file using RESTKit then in TOMCAT logs i get HTTP/1.1 200 213 status code and my file is not uploaded.

Here is my RESTKit code:

 RKObjectManager* manager = [RKObjectManager sharedManager]; 
RKObjectLoader* objectLoader = [manager objectLoaderWithResourcePath:@"/fileuploaded" delegate:self];
objectLoader.method = RKRequestMethodPOST;

UIImage *image = [UIImage imageNamed:@"rental_car_bill.jpeg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
// attach image 
RKParams *params = [RKParams paramsWithDictionary:(NSDictionary*)objectLoader.params]; 
RKParamsAttachment *attachment = [params setData:imageData 
                                        MIMEType:@"image/jpeg" forParam:@"photo"]; 
attachment.fileName = @"samplejpeg";
objectLoader.params = params;
objectLoader.targetObject = self;
[objectLoader send];

EDIT: Above implementation does work and the file does get uploaded. However, in the delegate method: - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

After it gets out of the scope of this, then my application crashes at [RKObjectLoader dealloc];

user598789
  • 329
  • 1
  • 2
  • 17

1 Answers1

0

To upload pictures using restkit I use the following methods, I prefer using blocks to avoid some problems on my app, maybe that way you can avoid your crash:

- (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    //pic.image returns an UIImage
    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    NSString *resourcePath = @"/api/upload/";


    //My API Server will return a JSON that represents my KFMedia Class after uploading the image, so here I get the propper mapping for that
    RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
        loader.method = RKRequestMethodPOST;
        loader.params = imageParams;
        [self settingsForLoader:loader withMapping:mapping onLoad:loadBlock onError:failBlock];

    }];

}

- (void) settingsForLoader: (RKObjectLoader *) loader withMapping: (RKObjectMapping *) mapping onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    loader.objectMapping = mapping;
    loader.delegate = self;
    loader.onDidLoadObject = loadBlock;
    loader.onDidFailWithError = ^(NSError * error){
        NSLog(@"%@",error);
    };
    loader.onDidFailLoadWithError = failBlock;
    loader.onDidLoadResponse = ^(RKResponse *response) {
        [self fireErrorBlock:failBlock onErrorInResponse:response];
    };

}
clopez
  • 4,372
  • 3
  • 28
  • 42