/    Sign up×
Community /Pin to ProfileBookmark

Reading parts of a very large textfile

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>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@PadonakJun 24.2013 — you are trying to get [B]myData[/B] beyond the function scope

<i>
</i>reader.onloadend = function(evt) [B][COLOR="#0000FF"][SIZE=3]{[/SIZE][/COLOR][/B]
if (evt.target.readyState == FileReader.DONE) [COLOR="#008000"][SIZE=3][B]{[/B][/SIZE][/COLOR]// DONE == 2
myData = "" + evt.target.result

[COLOR="#008000"][SIZE=3][B]}[/B][/SIZE][/COLOR]
alert("test1: " + myData);//Still contains the data
[COLOR="#0000FF"][SIZE=3][B]}[/B][/SIZE][/COLOR][B][COLOR="#FF0000"];[/COLOR][/B]
alert("test2: " + myData);//No more data.


p.s. use the code tags
Copy linkTweet thisAlerts:
@g_h_80authorJun 26.2013 — Padonak

Thank you for your answer. I already new it has to be something with a variable out of scope.

But I don't have any idea how to do this.

Can you give me a hint (=code example) on this one?

Thank you for the previous answer.

Hopefully you can help me even further.
Copy linkTweet thisAlerts:
@rnd_meJun 26.2013 — you can't return something that doesn't exist yet, you have to do what ever you need to do in the onload function, not outside of readblob function...
×

Success!

Help @g_h_80 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...