开发者

Decrypt Salesforce text blob Body via Javascript

I have some Javascript where I extract the Body of a static resource file:

function test() {
    var query = sforce.connection.query("Select Body from StaticResource where Name = 'StaticResourceFile'");
    var records = query.getArray("records");
    var body = records[0].Body;

I have access to the body now, but it appears to be encrypted. How do I decrypt it?

Via Apex, you can do it like this, but with Javascript not so:

Blob开发者_StackOverflow社区 blob = [Select Body from StaticResource where Name = 'StaticResourceFile'].Body;
string body = blob.toString();  // actual file contents!


If this is the object you are interacting with it looks like the data is probably Base64 encoded. You will need to decode it somehow. This question seems to cover a number of options to perform the decoding in Javascript.


Going off of bronsoja's answer, I searched around for a Salesforce-provided Base64 method. I found one, in the AJAX toolkit file 'connection.js':

<script type="text/javascript" src="/soap/ajax/22.0/connection.js" />
<script type="text/javascript">
   function test() {
      var test = sforce.connection.query("Select Body from StaticResource where Name = 'StaticResourceFile'");
      var records = test.getArray("records");
      var contents = sforce.Base64Binary.prototype.decode(records[0].Body);
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜