www.webdeveloper.com
Results 1 to 4 of 4

Thread: Reading parts of a very large textfile

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    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>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    you are trying to get myData beyond the function scope

    Code:
    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.
    p.s. use the code tags
    Last edited by Padonak; 06-24-2013 at 06:04 PM.

  3. #3
    Join Date
    Jun 2013
    Posts
    2
    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.

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles