Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop through a file, byte by byte, in JavaScript?

I need some help getting my head around how the file is accessed in JavaScript to do some operations on it.

I would like to loop through a file byte by byte using JavaScript. I can already select which file I would like to read. And I can read preset byte of the file.

I've found this nice example on how to read a slice of a file here:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

Here is the snippet of code which I'm playing with:

<style>
  #byte_content {
    margin: 5px 0;
    max-height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
  }
  #byte_range { margin-top: 5px; }
</style>

<input type="file" id="files" name="file" /> Read bytes: 
<span class="readBytesButtons">
  <button data-startbyte="0" data-endbyte="4">1-5</button>
  <button data-startbyte="5" data-endbyte="14">6-15</button>
  <button data-startbyte="6" data-endbyte="7">7-8</button>
  <button>entire file</button>
</span>
<div id="byte_range"></div>
<div id="byte_content"></div>

<script>
  function readBlob(opt_startByte, opt_stopByte) {

    var files = document.getElementById('files').files;
    if (!files.length) {
      alert('Please select a file!');
      return;
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;

    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        document.getElementById('byte_content').textContent = evt.target.result;
        document.getElementById('byte_range').textContent = 
            ['Read bytes: ', start + 1, ' - ', stop + 1,
             ' of ', file.size, ' byte file'].join('');
      }
    };

    var blob = file.slice(start, stop + 1);
    reader.readAsBinaryString(blob);
  }

  document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
      var startByte = evt.target.getAttribute('data-startbyte');
      var endByte = evt.target.getAttribute('data-endbyte');
      readBlob(startByte, endByte);
    }
  }, false);
</script>

Now I would like to loop through the file, four bytes at a time, but cannot seem to figure out how to do that. The reader does not seem to allow me to read more than once.

Once I can read from the file more than once, I should be able to iterate through it quite easily with something like this:

while( placemark != fileSize-4 ){
    output = file.slice(placemark, placemark + 4);      
    console.log(output);
    placemark = placemark + 5;
    }

Thanks in advance! Here is a link to a jsFiddle and plnkr version

like image 533
FredFury Avatar asked Jan 13 '15 10:01

FredFury


1 Answers

I'm not sure it is what you wanted but maybe it can help, and anyway I had fun.
I tried setting reader and file vars as global :

var reader = new FileReader(), step = 4, stop = step, start = 0, file;

document.getElementById('files').addEventListener('change', load, true);

function load() {
  var files = document.getElementById('files').files;
  file = files[0];
  reader.onloadend = function(evt) {
    if (evt.target.readyState == FileReader.DONE) {
      var result = evt.target.result;
      document.getElementById('byte_content').textContent += result; 
      document.getElementById('byte_range').textContent = ['Read bytes: ', start, ' - ', start+result.length,
        ' of ', file.size, ' byte file'
      ].join('');
    }
  }
}

function next() {
  if (!file) {
    alert('Please select a file!');
    return;
  }
  var blob = file.slice(start, stop);
  reader.readAsBinaryString(blob);

  start+= step;
  stop = start+step;
}

function loop() {
  if (!file) {
    alert('Please select a file!');
    return;
  }
  if (start < file.size) {
    next();
    setTimeout(loop, 50);
  }
}
<input type="file" id="files" name="file" />Read bytes:
<span class="readBytesButtons">
  <button onclick="next()">next</button>
  <button onclick="loop()">loop</button>
</span>
<div id="byte_range"></div>
<div id="byte_content"></div>
like image 111
Kaiido Avatar answered Sep 27 '22 16:09

Kaiido