1

我需要通过我的 iphone 应用程序将我的图像发送给 salesforce。我已经尝试过那些转换图像的东西--> 字节--> base64 编码 然后存储 sfdc(富数据字段),它做得很好,但我需要另存为图片。这是我在下面给出的代码(它不起作用)指导我如何实现

   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)来处理我的请求?

4

4 回答 4

1

NSData *myData = [[NSData alloc] 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");
}];
于 2013-12-10T15:21:12.650 回答
0

如果您只想将二进制数据上传到附件和/或文档,那么您根本不需要编写 Apex 类,您应该能够通过 REST API 完成所有操作。

链接的博客文章中显示的 Apex 类仅用于创建自定义 Web 服务。如果你愿意,你可以走这条路,但绝对不是你想要达到的要求。

于 2013-07-28T04:14:33.120 回答
0

我创建了顶点休息类来接收二进制数据,然后保存到 SFDC(保存在文档中的图像)。这是我提到的教程。

http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html

这里还有教程如何通过我的 ios 访问 apex 网络服务。

http://techblog.appirio.com/2013/07/rest-with-apex-service-in-native-ios.html

于 2013-07-29T05:07:15.113 回答
0

要使其查看功能适用于 Salesforce 管理 GUI 中附件中的图像,您还需要 ContentType 字段:

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];
于 2016-07-22T20:50:21.993 回答