JavaScript Get file input value as binary data file input value html,file input value jquery,file input

How can I access the binary representation with JavaScript from a file uploaded with a file input?:

<input type="file" name="file">

I can access the details of the uploaded file successfully with:

$('[name="image"]').get(0).files[0].name
// "2013-10-19 15.10.59.jpg"

$('[name="image"]').get(0).files[0].size
// 774016

$('[name="image"]').get(0).files[0].type
// "image/jpeg"

But not the real representation.

I found this tutorial that makes use of:

document.getElementById("photo").files[0].getAsBinary()

However, that method doesn't exists in my browser (Chrome Canary 34.0.1772.0 on OS X 10.9).

Answer:1

From https://developer.mozilla.org/en-US/docs/Web/API/File.getAsBinary

Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

It suggests:

Note: This method is obsolete; you should use the FileReader method readAsBinaryString() or readAsArrayBuffer() instead.

Using the FileReader:

//Retrieve the first (and only!) File from the FileList object
    var f = document.getElementById("photo").files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;
        alert("name: " + f.name + "n"
              +"type: " + f.type + "n"
              +"size: " + f.size + " bytesn"
              + "starts with: " + contents
        );  
      }
      r.readAsText(f);

However, please note that the FileReader API is still a working draft, and so this will not work in all browsers. As far as I am aware there is no currently supported way to do this, you could look into uploading the file with Ajax and reading its contents with a server side language?

Answer:2

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

  1. javascript some method
  2. javascript array some method
  3. javascript object some method

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

  1. click event dynamically created button jquery
  2. jquery click dynamically created element
  3. click on dynamically created button
  4. click function on dynamically created element
  5. click event on dynamically created element jquery
  6. jquery bind click dynamically created element
  7. click event for dynamically created button javascript
  8. click function for dynamically created button jquery
  9. click function on dynamically created element jquery
  10. click event for dynamically created button in asp.net c#
  11. click event for dynamically created li in jquery
  12. click event for dynamically created button c#
  13. trigger click on dynamically created element

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...