Newer Version Available

This content describes an older version of this product. View Latest

Working with Base64 Binary Encoded Strings

When working with Base64 encoded binary documents, you should access the document directly using the Id, rather than decoding Base64 in JavaScript.

Base64 encoding and decoding is very slow in JavaScript. Also, encoding and decoding does not work correctly for binary or multibyte strings. We do not recommend that you manipulate Base64 binary encoded strings with the AJAX Toolkit. However, if you want to read a document with Base64 binary encoding, you can use the API to query for the Id of the document and then download it directly from the server.

The following example demonstrates how to query for the document Id and then download it from the server:

1<html>
2<head>
3<script type="text/javascript" src="/js/dojo/0.3.1/dojo.js"></script>
4<script src="/soap/ajax/31.0/connection.js"></script>
5
6<script>
7function setup() {
8  var document_ta = document.getElementById("document-ta");
9
10  sforce.connection.query("select name, id from document limit 1",
11    {onSuccess : querySuccess,
12     onFailure : function(error, doc_ta) {
13        doc_ta.value = "Oops something went wrong: " + error;
14     },
15     source: document_ta});
16}
17
18function querySuccess(result, doc_ta) {
19  var records = result.getArray("records");
20
21  if (records.length == 1) {
22    dojo.io.bind({
23      url: "/servlet/servlet.FileDownload?file=" + records[0].Id,
24      load: loadDocument});
25  } else {
26    doc_ta.value = "no records found";
27  }
28}
29
30function loadDocument(type, data, event) {
31  var document_ta = document.getElementById("document-ta");
32  document_ta.value = data;
33}
34
35</script>
36</head>
37
38<body onload="setup()">
39<textarea id="document-ta" cols="80" rows="20">
40</textarea>
41</body>
42</html>

This example uses the JavaScript toolkit Dojo. For more information, see http://dojotoolkit.org/.

Note