Get base64 data of file from Salesforce

两盒软妹~` 提交于 2020-05-31 05:42:48

问题


I need to get opportunity files from Salesforce and copy them to some folder. I am using .NET library for connecting to Salesforcre. I can get any data I want, except the [VersionData] field in [ContentVersion] table, which contains base64 data of the files I want. I can get the data with Workbench tool, but the only thing I get via .NET library is a link to file. I could create HttpClient with appropriate headers and invoke that URL, but I don't like to go this ways. Can I get the file via .NET library?


回答1:


In REST API it has to be pulled through that url you got. It'll be a raw binary stream of data which would be tricky to represent together within JSON of normal query results. REST API is focused on mobile apps, minimizing network usage and base64 decode is some processing power I guess.

It shouldn't be tricky though? Just a GET to the URL you got with header Authorization: Bearer <session id here>

If you want base64 you need to make it a SOAP API request (which is what Workbench really uses, note that "REST explorer" is a separate menu option).

POST request to https://<redacted>.my.salesforce.com/services/Soap/u/48.0

with payload like

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
   <soapenv:Header>
      <urn:SessionHeader>
         <urn:sessionId>nice try ;) you can reuse same session id</urn:sessionId>
      </urn:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <urn:query>
         <urn:queryString>SELECT VersionData FROM ContentVersion WHERE Id = '068...'</urn:queryString>
      </urn:query>
   </soapenv:Body>
</soapenv:Envelope>

Will give you something like this back

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.partner.soap.sforce.com">
   <soapenv:Header>
      <LimitInfoHeader>
         <limitInfo>
            <current>12</current>
            <limit>5000000</limit>
            <type>API REQUESTS</type>
         </limitInfo>
      </LimitInfoHeader>
   </soapenv:Header>
   <soapenv:Body>
      <queryResponse>
         <result xsi:type="QueryResult">
            <done>true</done>
            <queryLocator xsi:nil="true"/>
            <records xsi:type="sf:sObject">
               <sf:type>ContentVersion</sf:type>
               <sf:Id xsi:nil="true"/>
               <sf:VersionData>/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/
(bla bla bla)
/X/lf0eG9Kl61//Z</sf:VersionData>
            </records>
            <size>1</size>
         </result>
      </queryResponse>
   </soapenv:Body>
</soapenv:Envelope>


来源:https://stackoverflow.com/questions/60283198/get-base64-data-of-file-from-salesforce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!