2013-07-24 40 views
1

我需要把我的形象在我的iphone app.I到Salesforce已經嘗試那些東西 轉換的圖像 - >字節 - > base64編碼然後存儲SFDC(豐富的數據字段),它是完全可以做到的,但我需要保存爲圖像。這裏是我的如下代碼(它不工作) 引導我如何實現這一發送附件到Salesforce通過iOS原生一個REST SDK

NSData *imageData = UIImagePNGRepresentation(imageView.image); 
    NSString *boundary = @"---------------------------14737809831466499882746641449"; 

    SFRestRequest *request = [[SFRestRequest alloc] init]; 
    [request setDelegate:self]; 
    [request setEndpoint:kSFDefaultRestEndpoint]; 
    [request setMethod:SFRestMethodPOST]; 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"entity_document\"; filename=\"%@\"\r\n",@"Test.png"]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: multipart/form-data\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Description :\"%s\"\r\n","Marketing brochure for Q1 2011"]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Keywords :\"%s\"\r\n","marketing,sales,update"]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"FolderId :\"%s\"\r\n","005D0000001GiU7"]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Name :\"%s\"\r\n","Marketing Brochure Q1"]] dataUsingEncoding:NSUTF8StringEncoding]]; 


    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSString *attbody = [NSString stringWithFormat:@"{\"body\" : {\"Name\" :[{ \"type\" : \"image/jpeg\", \"image\" : \"%@\"}] } }",body]; 

    [request setPath:[NSString stringWithFormat:@"/v23.0/sobjects/Document/"]]; 
    [request setQueryParams:(NSDictionary *)[SFJsonUtils objectFromJSONString:attbody]]; 
    [[SFRestAPI sharedInstance] send:request delegate:self]; 

有需要的任何類(APEX)處理我的請求?

+0

你是什麼意思關於圖像保存?在銷售隊伍文件或附件什麼的? –

+0

什麼,我需要從我的iPhone發送圖像SFDC? – Rams

+0

那什麼不起作用?你看到什麼錯誤? –

回答

0

如果你只是想上傳的二進制數據,附件和/或文件,那麼你不應該需要在所有寫的Apex類,你應該能夠通過REST API做的一切。如圖鏈接的博客文章

的Apex類只是用於創建自定義Web服務。如果你願意,你可以選擇這條路線,但對於你試圖達到的目標來說絕對不是必需的。

+0

我的客戶端請求,而不頂點類我不能能救我的圖像SFDC,但現在我可以用能夠頂點類保存我的形象 – Rams

+0

可以將其保存爲圖像,如果你使用一個文件或附件。如果使用文檔,請確保指定了正確的文件類型,否則它將無法按預期工作。 –

+0

我在這裏添加了我的答案。 – Rams

1

的NSData * myData的= [[NSData的頁頭]的init]; //初始化與文件內容

NSString *b64 = [myData base64EncodedString]; 

NSDictionary *fields = @{ 
         @"Name": @"Document Name.ext", 
         @"Body": b64, 
         @"ParentId": recordId 
         }; 
attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields]; 

[[SFRestAPI sharedInstance] sendRESTRequest:attachmentRequest failBlock:^(NSError *e) { 
    NSLog(@"Error"); 
} completeBlock:^(id dict){ 
    NSLog(@"Uploaded"); 
}]; 
+0

感謝這給了我需要得到它的提示。 –

0

爲了使這樣的視圖功能,Salesforce的適用於圖像附件你也想要ContentType字段的管理GUI:

UIImage *testImg = [UIImage imageNamed:@"someImageName"]; 
NSData *testImgData = UIImagePNGRepresentation(testImg); 
NSString *b64 = [testImgData base64Encode]; // base64Encode declared in NSData+SFAdditions.h 

NSDictionary *fields = @{ 
         @"Name": @"someImage.png", 
         @"Body": b64, 
         @"ParentId": @"00Q50000016TuGIEA0", // of course put your Attachment Id here instead of mine 
         @"ContentType": @"image/png" 
         }; 
SFRestRequest *attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields]; 
[[SFRestAPI sharedInstance] send:attachmentRequest delegate:self];