Hi everyone
I want to read parts of a very large textfile.
I succeed in it (thanks to some code I’ve found and slaightly changed)!
But I can’t get my data out of the function.
I can see the data at alertbox “test1”, but at alertbox “test2” the code stops!!
Can anyone tell me what I’m doing wrong and make a code suggestion to solve my problem?
Thanks in advanced.
This is the code that I use:
<!DOCTYPE html>
<html>
<head>
<title>Reading local files in JavaScript – HTML5 Rocks</title>
</head>
<body onload=”initialize()”>
<h1>Slicing a file</h1>
<input type=”file” id=”files4″ name=”file” />
<span class=”readBytesButtons”>
<button>Get part of file !</button>
</span>
<script>
document.querySelector(‘.readBytesButtons’).addEventListener(‘click’, function(evt) {
readBlob(100000000, 100000010)
}, false);
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById(‘files4’).files;
if (!files.length) {
alert(‘Please select a file!’);
return;
}
var file = files[0];
var start = parseInt(opt_startByte);
var stop = parseInt(opt_stopByte);
var blob = file.slice(start, stop + 1);
var reader = new FileReader();
reader.readAsBinaryString(blob);
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) {// DONE == 2
myData = “” + evt.target.result
}
alert(“test1: ” + myData);//Still contains the data
};
alert(“test2: ” + myData);//No more data.
return myData;//No more data.
}
</script>
</body>
</html>