title: How to put png binary data into an img tag and display it as an image?
body: <p>I am using this</p>
  
  <pre><code>        $.ajax({
              type: "GET",
              url: 'template/bump1/purse.png',
              datatype:"image/png",
              success: function (data) {
  
  
                  var reader = new FileReader();
  
                  reader.onload = function (e) {
                    var img = document.getElementById("CaptchaImg");
                    img.src = e.target.result;
                  };
                  reader.readAsDataURL(data);
  
  
                  //$('#CaptchaImg').attr('src', data);
              }
           });
  </code></pre>
  
  <p>to download an image, and it comes out in binary, looking like this</p>
  
  <p><a href="https://i.stack.imgur.com/gCaGY.png"><img src="https://i.stack.imgur.com/gCaGY.png" alt="enter image description here"></a></p>
  
  <p>node.js is returning it as</p>
  
  <pre><code>                        WriteHeaderMode('image/png', res, 200);
                          res.end(data, 'binary');
  </code></pre>
  
  <p>But now, how do I put that into an image tag and show it as an image. Note: I <strong>do not</strong> want to have return data as base64 encoding, it has to be binary. Im fine with converting the binary into base64 on client side though.</p>
  
  <p>When I pass it to the <code>readAsDataURL</code>, it says <code>TypeError</code> exception.</p>
  
  <p>Thanks</p>
  
  <p>EDIT</p>
  
  <pre><code>                var img = document.getElementById("CaptchaImg");
  
                    var reader = new FileReader();
  
                    reader.onload = function(e) {
                        //img.src = e.target.result;
                        $("body").html(e.target.result);
                      };
  
                    reader.readAsDataURL(new Blob([data]));
  </code></pre>
  
  <p>this seems to convert it into a base64 encoding, which starts as <code>data:application/octet-stream;base64,</code> but doesn't display an image...</p>