Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script type='text/javascript'>
- function init()
- {
- // own ajax library - using it to request a test jpg image
- new Ajax().sendRequest
- (
- "/images/photos/badger.jpg",
- { method : "GET",
- callback: function(xmlHTTP)
- {
- var encoded = btoa (unescape(encodeURIComponent(xmlHTTP.responseText)));
- var dataURL="data:image/jpeg;base64,"+encoded;
- document.getElementById("image").src = dataURL;
- }
- }
- );
- }
- </script>
- <script type="text/javascript" src="http://www.free-map.org.uk/0.6/js/lib/Ajax.js"></script>
- </head>
- <body onload='init()'>
- <img id="image" alt="data url loaded image" />
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <script type='text/javascript'>
- function init()
- {
- var xmlHTTP = new XMLHttpRequest();
- xmlHTTP.open('GET','/images/photos/badger.jpg',true);
- // Must include this line - specifies the response type we want
- xmlHTTP.responseType = 'arraybuffer';
- xmlHTTP.onload = function(e)
- {
- var arr = new Uint8Array(this.response);
- // Convert the int array to a binary string
- // We have to use apply() as we are converting an *array*
- // and String.fromCharCode() takes one or more single values, not
- // an array.
- var raw = String.fromCharCode.apply(null,arr);
- // This works!!!
- var b64=btoa(raw);
- var dataURL="data:image/jpeg;base64,"+b64;
- document.getElementById("image").src = dataURL;
- };
- xmlHTTP.send();
- }
- </script>
- </head>
- <body onload='init()'>
- <img id="image" alt="data url loaded image" />
- </body>
- </html>
- var raw = String.fromCharCode.apply(null,arr);
- var raw = '';
- var i,j,subArray,chunk = 5000;
- for (i=0,j=arr.length; i<j; i+=chunk) {
- subArray = arr.subarray(i,i+chunk);
- raw += String.fromCharCode.apply(null, subArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement