Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type='text/javascript'>
  5.  
  6. function init()
  7. {
  8. // own ajax library - using it to request a test jpg image
  9. new Ajax().sendRequest
  10. (
  11. "/images/photos/badger.jpg",
  12. { method : "GET",
  13. callback: function(xmlHTTP)
  14. {
  15.  
  16. var encoded = btoa (unescape(encodeURIComponent(xmlHTTP.responseText)));
  17. var dataURL="data:image/jpeg;base64,"+encoded;
  18.  
  19. document.getElementById("image").src = dataURL;
  20. }
  21. }
  22. );
  23. }
  24.  
  25. </script>
  26. <script type="text/javascript" src="http://www.free-map.org.uk/0.6/js/lib/Ajax.js"></script>
  27. </head>
  28. <body onload='init()'>
  29. <img id="image" alt="data url loaded image" />
  30. </body>
  31. </html>
  32.  
  33. <!DOCTYPE html>
  34. <html>
  35. <head>
  36. <script type='text/javascript'>
  37.  
  38. function init()
  39. {
  40. var xmlHTTP = new XMLHttpRequest();
  41. xmlHTTP.open('GET','/images/photos/badger.jpg',true);
  42.  
  43. // Must include this line - specifies the response type we want
  44. xmlHTTP.responseType = 'arraybuffer';
  45.  
  46. xmlHTTP.onload = function(e)
  47. {
  48.  
  49. var arr = new Uint8Array(this.response);
  50.  
  51.  
  52. // Convert the int array to a binary string
  53. // We have to use apply() as we are converting an *array*
  54. // and String.fromCharCode() takes one or more single values, not
  55. // an array.
  56. var raw = String.fromCharCode.apply(null,arr);
  57.  
  58. // This works!!!
  59. var b64=btoa(raw);
  60. var dataURL="data:image/jpeg;base64,"+b64;
  61. document.getElementById("image").src = dataURL;
  62. };
  63.  
  64. xmlHTTP.send();
  65. }
  66.  
  67. </script>
  68. </head>
  69. <body onload='init()'>
  70. <img id="image" alt="data url loaded image" />
  71. </body>
  72. </html>
  73.  
  74. var raw = String.fromCharCode.apply(null,arr);
  75.  
  76. var raw = '';
  77. var i,j,subArray,chunk = 5000;
  78. for (i=0,j=arr.length; i<j; i+=chunk) {
  79. subArray = arr.subarray(i,i+chunk);
  80. raw += String.fromCharCode.apply(null, subArray);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement