0

I need to create extract the signature of a file at the client level itself so as to positively determine its file type. Below is my file input object:

<input id="test1" type="file">

I wrote the following javascript code against it:

var fileInput = document.getElementById('test1');
fileInput.addEventListener('change', function(e) {
    console.log("file selected");
    var reader = new FileReader();
    reader.onload = function(e) {
        console.log("loaded");
        var file_slice = gcUploadFile.slice(0,4);
        console.log(file_slice);
        var arr_buffer = reader.readAsArrayBuffer(file_slice);
        console.log(arr_buffer);
    }
});

Check out the fiddle for the above.

The trouble I am having is that my code does not even enters the onload fucntion.

What am i doing wrong?

Note: I am coding only using plain javascript but i am open to use Google Closure.

1 Answer 1

1

Why would it reach the onload handler, nothing is ever read by the FileReader.

You have to pass the file to the fileReader by reading it as something

var fileInput = document.getElementById('test1');
fileInput.addEventListener('change', function(e) {
    console.log("file selected");
    var reader = new FileReader();
    reader.onload = function(e) {
        console.log("loaded");
        var file_slice = gcUploadFile.slice(0,4);
        console.log(file_slice);
        var arr_buffer = reader.readAsArrayBuffer(file_slice);
        console.log(arr_buffer);
    }

    reader.readAsBinaryString(e.target.files[0]);

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.