Wasim
Wasim

Reputation: 5113

How to upload a file using Reskit in iOS

How would I go about uploading an audio file using Restkit? I've looked around the documentation and can't seem to find anything about uploading files. Thanks for your help.

Upvotes: 2

Views: 3526

Answers (2)

gdm
gdm

Reputation: 7958

For Restkit 0.20, the MIME types are automatically detected when creating the attachement. So you do not need to call

attachment.MIMEType

And when building the params:

[params setData:imageData MIMEType:attachment.MIMEType forParam:@"image2"];

see this link

Upvotes: 0

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44808

This article explains how: http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/

Excerpt:

NSString* myFilePath = @"/some/path/to/picture.gif";
RKParams* params = [RKParams params];

// Set some simple values -- just like we would with NSDictionary
[params setValue:@"Blake" forParam:@"name"];
[params setValue:@"[email protected]" forParam:@"email"];

// Create an Attachment
RKParamsAttachment* attachment = [params setFile:myFilePath forParam:@"image1"];
attachment.MIMEType = @"image/gif";
attachment.fileName = @"picture.gif";

// Attach an Image from the App Bundle
UIImage* image = [UIImage imageNamed:@"another_image.png"];
NSData* imageData = UIImagePNGRepresentation(image);
[params setData:imageData MIMEType:@"image/png" forParam:@"image2"];

// Let's examine the RKRequestSerializable info...
NSLog(@"RKParams HTTPHeaderValueForContentType = %@", [params HTTPHeaderValueForContentType]);
NSLog(@"RKParams HTTPHeaderValueForContentLength = %d", [params HTTPHeaderValueForContentLength]);

// Send a Request!
[[RKClient sharedClient] post:@"/uploadImages" params:params delegate:self];

Enjoy!

Upvotes: 4

Related Questions