Dave Bormann
Dave Bormann

Reputation: 43

RestKit image upload parameters

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"
}

Upvotes: 0

Views: 1227

Answers (1)

Jeroen Coup&#233;
Jeroen Coup&#233;

Reputation: 1404

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.

Upvotes: 1

Related Questions