0

I've made multiple attempts to enable POST image file transfer with RestKit but have only succeeded so far using curl. The working code is below, but it is synchronous and makes the UI unresponsive.

NSArray *arguments =
[NSArrayarrayWithObjects:assetScriptFullPath,
  @"-F", [NSString stringWithFormat:@"asset[file]=@%@", fullPath],
  @"-F", [NSString stringWithFormat:@"asset[user_id]=%d", user_id],
  @"-F", [NSString stringWithFormat:@"asset[checksum]=%s", [(NSString *)md5hash UTF8String]],
  nil];
NSTask *task = [NSTasklaunchedTaskWithLaunchPath:@"/usr/bin/curl"arguments:arguments];

The curl call is received at the server as below:

{
"asset"=>{
  "file"=>#<ActionDispatch::Http::UploadedFile:0xcfcc630
    @original_filename="IMG_6236.JPG",
    @content_type="image/jpeg",
    @headers="Content-Disposition: form-data; name=\"asset[file]\"; filename=\"IMG_6272.JPG\"\r\nContent-Type: image/jpeg\r\n",
    @tempfile=#<File:/tmp/RackMultipart20121117-21489-brm3b9>>,
  "user_id"=>"522",
  "checksum"=>"ab23bc492bac990d9022248315c743c1"
  }
}

One attempt with RestKit is based on this post ( RestKit Image Upload ) but doesn't nest file within asset. My attempts to nest the params within 'asset' haven't worked or have crashed.

{
  "user_id"=>"522",
  "checksum"=>"ab23bc492bac990d9022248315c743c1",
  "file"=>#<ActionDispatch::Http::UploadedFile:0x883eb9c
    @original_filename="file",
    @content_type="image/jpeg",
    @headers="Content-Disposition: form-data; name=\"file\"; filename=\"file\"\r\nContent-Type: image/jpeg\r\n",
    @tempfile=#<File:/tmp/RackMultipart20121125-25702-ac8ck9>>
}

Using the method described in the RestKit advanced tutorial below I either can't get the hierarchy I need (file within asset) or I can't get the image data attached without crashing. https://github.com/RestKit/RestKit/blob/master/Docs/MobileTuts%20Advanced%20RestKit/Advanced_RestKit_Tutorial.md One way I tried to attach the image that causes crashes is described here: Serialize nested image in RestKit (Rails Backend)

{
"asset"=>{
  "user_id"=>"522", 
  "file"=>"@/Users/dev/IMG_6236.JPG", 
  "checksum"=>"ab23bc492bac990d9022248315c743c1"
  }
}

Any recommendations? Thanks!


If I could change what the server expects I can get it to work with a flat parameter hierarchy. This isn't a solution though, I can't change the hierarchy. The code is below:

[params setFile:asset forParam:@"file"];
[params setData:[name dataUsingEncoding:NSUTF8StringEncoding] forParam:@"name"];
[params setData:[user_id dataUsingEncoding:NSUTF8StringEncoding] forParam:@"user_id"];
[client post:assetScriptPath params:params delegate:self];

This is what the server sees, but I need this all within an "asset" as above.

{
  "file"=>#<ActionDispatch::Http::UploadedFile:0xcfcc630
    @original_filename="IMG_6236.JPG",
    @content_type="image/jpeg",
    @headers="Content-Disposition: form-data; name=\"file\"; filename=\"IMG_6272.JPG\"\r\nContent-Type: image/jpeg\r\n",
    @tempfile=#<File:/tmp/RackMultipart20121117-21489-brm3b9>>,
  "user_id"=>"522",
  "checksum"=>"ab23bc492bac990d9022248315c743c1"
}
Community
  • 1
  • 1

1 Answers1

1

The way we do it is we encode the image into base64Encoding and decode it on the server side.

Something like this.

NSData *imageData = [NSData dataWithData:imageForUpload];
NSString *encodedString = [imageData base64Encoding];

After that it works just like a regular RestKit post.

Jeroen Coupé
  • 1,394
  • 11
  • 13
  • Thanks for your quick response! I'm getting an error that there is no base64Encoding selector. What am I doing wrong? – Dave Bormann Nov 26 '12 at 09:14
  • Should've mentioned it. I'm using the Base64 additions from MGTwitterEngine.https://github.com/mattgemmell/MGTwitterEngine/blob/master/NSData%2BBase64.m – Jeroen Coupé Nov 26 '12 at 09:45
  • Thanks, I got it to send the base64 encoded image. Unfortunately I can't change the server side since other things depend on it. – Dave Bormann Nov 28 '12 at 09:41
  • Would this work better than RestKit? https://github.com/AFNetworking/AFNetworking – Dave Bormann Nov 28 '12 at 10:01
  • The scope of AFNetworking is not as broad as restkit. restkit does networking, but also does the mapping of the objects to classes (and to core data). – Jeroen Coupé Nov 28 '12 at 10:45
  • Can RestKit do what I'm trying to do though? If not I'll have to switch to something else. Also noticed a problem with unicode characters in filename with RestKit, doesn't handle them properly in @headers "filename". – Dave Bormann Nov 28 '12 at 20:35