【问题标题】:Send attachments to salesforce native rest sdk through ios通过ios发送附件到salesforce native rest sdk
【发布时间】:2013-07-24 08:47:17
【问题描述】:

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

【问题讨论】:

  • 另存为图片是什么意思?在 Salesforce 文档或附件或其他内容中?
  • 无论如何,我需要从我的 iphone 发送图像到 sfdc?
  • 那么什么不工作?你看到了什么错误?
  • 我的疑问是我是否需要创建一个顶点类来访问我的客户端请求,例如blogs.developerforce.com/developer-relations/2011/09/…

标签: iphone ios objective-c salesforce


【解决方案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");
}];

【讨论】:

  • 谢谢,这给了我获得它所需的提示。
【解决方案2】:

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

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

【讨论】:

  • 没有 apex 类我无法将我的图像保存到 sfdc 但现在我可以使用 apex 类保存我的图像
  • 如果您使用文档或附件,您可以将其保存为图像。如果您使用文档,请确保指定正确的文件类型,否则它将无法按预期工作。
  • 我已经在这里添加了我的答案。
【解决方案3】:

我创建了 apex rest 类来接收二进制数据,然后保存到 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

【讨论】:

    【解决方案4】:

    要使查看功能适用于 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];
    

    【讨论】:

      猜你喜欢
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多