1

Is it possible to view the raw data for an image file in javascript?

I'm trying to write a script that converts an image into its hex dump.

How can I view the data I'm writing to the image file?

jahroy
  • 22,322
  • 9
  • 59
  • 108
Philipp Braun
  • 1,583
  • 2
  • 25
  • 41
  • 1
    I'd request it with AJAX and read the resulting data...but I'm not sure if there is a better solution. – JCOC611 Oct 05 '12 at 22:28
  • You need to precise where you are working ,client side or server side ? check the file readers and typed arrays server side. – mpm Oct 05 '12 at 22:35
  • I found a source code viewer here http://jawjahboy.com/utility/ but I guess it works with ajax. – Philipp Braun Oct 05 '12 at 22:35
  • The main problem is that I have no idea how to get the source code into a string!! – Philipp Braun Oct 05 '12 at 22:36
  • 1
    Actually, your problem is that you don't know what the term _source code_ means. Source code is what you write when you create a program. You're looking to get the _raw image data_. – jahroy Oct 05 '12 at 22:52

2 Answers2

2

You can do this with XHR:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/file.png', true);
xhr.responseType = 'arraybuffer'; // this will accept the response as an ArrayBuffer
xhr.onload = function(buffer) {
    var words = new Uint32Array(buffer),
        hex = '';
    for (var i = 0; i < words.length; i++) {
      hex += words.get(i).toString(16);  // this will convert it to a 4byte hex string
    }
    console.log(hex);
};
xhr.send();

Look at the doc for ArrayBuffers and TypedArrays

And you can see how I use it in testing here

saml
  • 6,702
  • 1
  • 34
  • 30
0

Is it possible to get the source code of a file in javascript?

Find the URL for the script and load that into your browser. (Or use Firebug)

Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55