Reading a File and Getting its Binary Data in Javascript

Use-Cases of this code snippet

  • Know how to get binary data of a file selected by user.

The binary data of a local file selected by the user can be retrieved using the readAsBinaryString method of a FileReader object.

Live Demo

Binary Data


<input type="file" id="file" />
<button id="read-file">Read File</button>


document.querySelector("#read-file").addEventListener('click', function() {
	// no file selected to read
	if(document.querySelector("#file").value == '') {
		console.log('No file selected');

	var file = document.querySelector("#file").files[0];

	var reader = new FileReader();
	reader.onload = function(e) {
		// binary data
	reader.onerror = function(e) {
		// error occurred
		console.log('Error : ' + e.type);


The readAsBinaryString method of a FileReader object asynchronously reads the file. The load event is fired once reading is finished and raw binary data is available.

How to Read Local Files with Javascript
Loading Comments