如何在iOS中使用Reskit上传文件

时间:2012-08-30 14:29:25

标签: objective-c ios restkit

如何使用Restkit上传音频文件?我查看了文档,似乎无法找到有关上传文件的任何信息。谢谢你的帮助。

2 个答案:

答案 0 :(得分:4)

本文解释了如何:http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/

摘录:

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:@"blake@restkit.org" 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];

享受!

答案 1 :(得分:0)

对于Restkit 0.20,在创建附件时会自动检测MIME类型。所以你不需要打电话

attachment.MIMEType

建立params时:

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

请参阅此link