0

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 blob = [Select Body from StaticResource where Name = 'StaticResourceFile'].Body;
string body = blob.toString();  // actual file contents!
dotNetkow
  • 5,053
  • 4
  • 35
  • 50

2 Answers2

1

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.

Community
  • 1
  • 1
bronsoja
  • 799
  • 4
  • 8
1

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>
dotNetkow
  • 5,053
  • 4
  • 35
  • 50