我有一个从视图下载的文档属性,所以我有来自文档的实际JSON,包括ID和Rev ID,但我没有实际的CouchDocument。
这份文件有一个附件,我知道它的名字。我正在尝试将附件下载到CouchAttachment对象中,但我找不到一种方法,可以在不重新下载文档的情况下完成此操作,这很慢。这就是我要做的:
-(CouchAttachment *)getAttachmentFor:(NSObject *)doc named:(NSString *)fileName {
if ([[doc valueForKey:@"_attachments"] valueForKey:fileName]==nil)
return nil;
CouchDocument * document = [[[App shared] database] documentWithID:[doc valueForKey:@"_id"]];
CouchRevision * revision = [document revisionWithID:[doc valueForKey:@"_rev"]];
return [revision attachmentNamed:fileName];
}有没有什么方法可以直接获取附件,而不必先获取文档和版本?
发布于 2012-05-08 14:26:23
CouchCocoa框架似乎没有提供直接创建CouchAttachment对象的方法。但是,如果您知道附件的URL,则可以使用get操作直接获取附件。
假设您在某个数据库中有一些文档,其中包含一个名为someAttachment.txt附件。附件URL格式为:
http://couchdb/someDatabase/someDocumentID/someAttachment.txt?rev=<your revision id>您的修订ID和文档ID来自您的doc字典。如果您可以传递服务器URL和/或数据库URL,则可以执行GET操作来获取附件,如下所示。
RESTResource *aRestResource=[[RESTResource alloc] initWithURL:[NSURL URLWithString:@"http://couchdb/someDatabase/someDocumentID/someAttachment.txt?rev=<your revision id>"]];
[aRestResource autorelease];
RESTOperation *aRestOperation=[aRestResource GET];
[aRestOperation onCompletion:^{
NSLog(@"Content Type:%@",aRestOperation.responseBody.contentType);
//The response for the GET will contain the attachment's data. You can
NSData *contentData=[[NSData alloc] initWithData:aRestOperation.responseBody.content];
NSString *contentString=[[NSString alloc] initWithData:contentData encoding:NSUTF8StringEncoding];
NSLog(@"Content:%@",contentString); //Should contain the text in someAttachment.txt
[contentData release];
[contentString release];
}];
[aRestOperation wait];来源:http://wiki.apache.org/couchdb/HTTP_Document_API#Attachments
或者,您可以使用RESTResource的initWithURL:方法创建一个CouchAttachment对象,但该方法不会构造特定于CouchAttachment的属性,如文档和数据库等。
https://stackoverflow.com/questions/10074933
复制相似问题