2012-09-26 86 views
7

問題

我一直在試圖通過包含圖像附件的多種請求發佈到服務器。我沒有遇到麻煩的圖像到服務器,這是其他信息不正確發送。Restkit MultiForm Post with a Image

詳細

我使用對象映射從服務器接收對象時,配置多個不同的屬性:

//Using a custom class to map object I receive to 
RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]]; 
[memoryMapper mapAttributes:@"created", @"user", @"participants", @"tags", @"text", @"kind", @"video", @"location", nil]; 
[memoryMapper mapKeyPath:@"_id" toAttribute:@"memoryID"]; 

//MediaMapper handles the data needed for the Image attachments 
RKObjectMapping * mediaMapper = [RKObjectMapping mappingForClass:[MemoMedia class]]; 
[mediaMapper mapKeyPath:@"processed" toAttribute:@"processed"]; 
[mediaMapper mapKeyPath:@"original" toAttribute:@"original"]; 
[mediaMapper mapKeyPath:@"mime" toAttribute:@"mimeType"]; 
[memoryMapper mapKeyPath:@"media" toRelationship:@"rawMedia" withMapping:mediaMapper]; 

// 
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memories"]; 
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeFormURLEncoded; 
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON; 

然後,當談到時間將照片張貼到我更新的配置如下:

RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]]; 
[memoryMapper mapAttributes:@"created", @"participants", nil]; 
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:memoryMapper forClass:[MemoContent class]]; 
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memory"]; 

與會人員標有照片。下面是我如何張貼,類似這樣的https://github.com/RestKit/RestKit/wiki/Attach-a-File-to-an-RKObjectLoader

[[RKObjectManager sharedManager] postObject:theMemory usingBlock:^(RKObjectLoader * loader){ 

    RKObjectMapping* serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[MemoContent class]]; 
    NSLog(@"serializationMapping: %@", serializationMapping); 
    loader.delegate = APP; //main app delegate posting, updating 
    NSError* error = nil; 
    RKObjectSerializer * serializer = [[RKObjectSerializer alloc] initWithObject:theMemory mapping:serializationMapping]; 
    NSDictionary * dictionary = [serializer serializedObject:&error]; 
    RKParams * params = [RKParams paramsWithDictionary:dictionary]; 
    NSData * imageData = UIImagePNGRepresentation(theMemory.photo); //image data 
    [params setData:imageData MIMEType:@"image/png" forParam:@"attachment"]; 
    loader.params = params; 
    loader.serializationMIMEType = RKMIMETypeFormURLEncoded; 
    }]; 

服務器接收圖像按計劃,實際上確實接收到「創造」和「參與者」不幸的是它是在一個陌生的格式,服務器不明白。它包括換行符和participants (\n 19843589323 \n created: \n 3-31-2012 00:00(類似的東西,我將在更新日誌時訪問日誌。)

我會給你任何額外的信息,如果我有足夠的信息, )

+0

你的意思是與內容型'一個POST multipar噸/形狀data'?你堅持使用RestKit嗎? – leo

+0

我寧願堅持使用Restkit,而且我確實相信多部分/表單數據是正確的..但是您能否詳細說明一下? –

+0

無法幫助您使用RestKit,但如果這不是必需的,則可以使用許多其他解決方案。今天早上我在網上發佈了一個多部分/表單數據的遊戲,看看[https://gist.github.com/3786554](https://gist.github.com/3786554) – leo

回答

14

RestKit 0.20.0-pre3RKObjectManager確實有方法multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

+0

我實際上最終使用這種技術,愚蠢的是我沒有把它添加到接受的答案。謝謝。 –

11

這個任務的例子可以在RestKit Github page發現:

Article *article = [Article new]; 
UIImage *image = [UIImage imageNamed:@"some_image.png"]; 

// Serialize the Article attributes then attach a file 
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:article method:RKRequestMethodPOST path:nil parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    [formData appendPartWithFileData:UIImagePNGRepresentation(image) 
           name:@"article[image]" 
          fileName:@"photo.png" 
          mimeType:@"image/png"]; 
}]; 

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil]; 
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation]; // NOTE: Must be enqueued rather than started 
+0

爲什麼排隊而不是開始? – fellowworldcitizen