I need help to understand how a file is accessed in JavaScript to do some operations on it.
I would like to skip the byte byte file using JavaScript. I can already choose which file I would like to read. And I can read the given byte of the file.
I found this good example of how to read a file fragment here:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Here is the code snippet 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(); </script>
Now I would like to skip the file, four bytes at a time, but cannot figure out how to do this. The reader does not seem to allow me to read more than once.
As soon as I can read from a file more than once, I should be able to do 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 jsFiddle and plnkr version
source share