title: Display thumbnailPhoto from Active Directory using Javascript only - Base64 encoding issue
body: <p>Here's what I'm trying to do:
  From an html page using only Javascript I'm trying to query the Active Directory and retrieve some user's attributes.
  Which I succeded to do (thanks to some helpful code found around that I just cleaned up a bit).
  I can for example display on my html page the "displayName" of the user I provided the "samAccountName" in my code, which is great.</p>
  
  <p>But I also wanted to display the "thumbnailPhoto" and here I'm getting some issues...
  I know that the AD provide the "thumbnailPhoto" as a byte array and that I should be able to display it in a  tag as follow:</p>
  
  <pre><code>&lt;img src="data:image/jpeg;base64," /&gt;
  </code></pre>
  
  <p>including base64 encoded byte array at the end of the src attribute.</p>
  
  <p>But I cannot manage to encode it at all.
  I tried to use the following library for base64 encoding:
  <a href="https://github.com/beatgammit/base64-js" rel="nofollow">https://github.com/beatgammit/base64-js</a></p>
  
  <p>But was unsuccesful, it's acting like nothing is returned for that AD attribute, but the photo is really there I can see it over Outlook or Lync.
  Also when I directly put that returned value in the console I can see some weird charaters so I guess there's something but not sure how it should be handled.</p>
  
  <p>Tried a typeof to find out what the variable type is but it's returning "undefined".</p>
  
  <p>I'm adding here the code I use:</p>
  
  <pre><code>var ADConnection = new ActiveXObject( "ADODB.connection" );
  var ADCommand = new ActiveXObject( "ADODB.Command" );
  ADConnection.Open( "Data Source=Active Directory Provider;Provider=ADsDSOObject" );
  ADCommand.ActiveConnection = ADConnection;
  
  var ou = "DC=XX,DC=XXXX,DC=XXX";
  var where = "objectCategory = 'user' AND objectClass='user' AND samaccountname='XXXXXXXX'";
  var orderby = "samaccountname ASC";
  var fields = "displayName,thumbnailPhoto";
  var queryType = fields.match( /,(memberof|member),/ig ) ? "LDAP" : "GC";
  var path = queryType + "://" + ou;
  
  ADCommand.CommandText = "select '" + fields + "' from '" + path + "' WHERE " + where + " ORDER BY " + orderby;
  
  var recordSet = ADCommand.Execute;
  fields = fields.split( "," );
  var data = [];
  while(!recordSet.EOF)
  {
      var rowResult = { "length" : fields.length };
      var i = fields.length;
  
      while(i--)
      {
          var fieldName = fields[i];
  
          if(fieldName == "directReports" &amp;&amp; recordSet.Fields(fieldName).value != null)
          {
              rowResult[fieldName] = true;
          }
          else
          {
              rowResult[fieldName] = recordSet.Fields(fieldName).value;
          }
      }
      data.push(rowResult);
      recordSet.MoveNext;
  }
  recordSet.Close();
  
  console.log(rowResult["displayName"]);
  console.log(rowResult["thumbnailPhoto"]);
  </code></pre>
  
  <p>(I replaced db information by Xs)</p>
  
  <p>(There's only one entry returned that's why I'm using the rowResult in the console instead of data)</p>
  
  <p>And here's what the console returns:</p>
  
  <blockquote>
    <p>LOG: Lastname, Firstname</p>
    
    <p>LOG: က䙊䙉Āā怀怀</p>
  </blockquote>
  
  <p>(same here Lastname &amp; Firstname returned are the correct value expected)</p>
  
  <p>This is all running on IE9 and unfortunetly have to make this compatible with IE9 :/</p>
  
  <p>Summary:</p>
  
  <ul>
  <li>I need to find a solution in Javascript only</li>
  <li>I know it should be returning a byte array and I need to base64 encode it, but all my attempts failed and I'm a bit clueless on the reason why</li>
  <li>I'm not sure if the picture is getting returned at all here, the thing in the console seems pretty small... or if I'm nothing doing the encoding correctly</li>
  </ul>
  
  <p>If someone could help me out with this it would be awesome, I'm struggling with this for so long now :/</p>
  
  <p>Thanks!</p>