Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:24:12+00:00 2026-06-16T05:24:12+00:00

I am doing something similar to http://www.html5rocks.com/en/tutorials/file/dndfiles/ What I’m doing is Im reading the

  • 0

I am doing something similar to http://www.html5rocks.com/en/tutorials/file/dndfiles/

What I’m doing is Im reading the contents of the selected files one at a time to validate that their lines pass some regex test. After done validating all files, I need to update (enable / disable) some buttons accordingly hence the call back function

Is it possible to have a call back function which will do something after everything is read?

HTML:

<input type="file" id="files" name="files[]" multiple />

Javascipt:

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // files is a FileList of File objects. List some properties.
    var validArray = [];
    for (var i = 0, f; f = files[i]; i++) {
        //Create new file reader
        var r = new FileReader();
        //On load call
        r.onload = (function (f) {
            return function (e) {
                var contents = e.target.result;
                var lines = contents.split('\n');
                
                for(var i=0; i<lines.length; i++){
                    //Validate regex of line here
                    //If line does not pass, append file name to validArray and break  
                }
                                
            };
        })(f);
        r.readAsText(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:24:14+00:00Added an answer on June 16, 2026 at 5:24 am

    Came here looking for a similar answer. I wanted to call a function after all files were loaded and processed. The solution provided by @Snuffleupagus did not work for me because the function was called after all the files were read, but before they had finished being processed in the onload function. I found a solution around this as follows (not sure if it is the ‘cleanest’ but it works for me).

    var processedCount=0; // global variable
    var totalFiles = 0; // global variable
    
    function handleFileSelect(evt) {
      var files = evt.target.files; // FileList object
    
      totalFiles = files.length; // important
    
      // files is a FileList of File objects. List some properties.
      for (var i = 0, f; f = files[i]; i++) {
        //Create new file reader
        var r = new FileReader();
        //On load call
        r.onload = (function(theFile){
            return function(){
              onLoadHandler(this,theFile);
              onLoadEndHandler();
           };
        })(f);
        r.readAsText(f);
      }
    }
    
    function onLoadEndHandler(){
      processedCount++;
      if(processedCount == totalFiles){ 
        // do whatever - this code will run after everything has been loaded and processed
      }
    }
    

    I tried to use r.onloadend but it was called too soon. I believe because my function ‘onLoadHandler’ takes a few seconds to process each file and onloadend is called when the file is done being loaded but before the code within ‘onload’ has finished running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing something quite similar to the tutorial http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive . I now want to
I am in the process of making something similar to this: http://www.artversion.com/pdf/WebsiteDesignQuestionnaires.pdf I have
This seems possible as http://www.knockoutjs.com appears to be doing it. I haven't been able
So I am using the google code prettifier: http://code.google.com/p/google-code-prettify/ It does something similar to
I'm doing something very similar to the stackoverflow question preview only much more basic.
Quick Question How does youtube encode theirs urls? take below http://www.youtube.com/watch?v=MhWyAL2hKlk what are they
I want to know how is the Mozenda Screen Scraper coded? http://www.mozenda.com/screen-scraper I shows
So I want to match just the domain from ether: http://www.google.com/test/ http://google.com/test/ http://google.net/test/ Output
I am following the advice from this webpage: http://www.formortals.com/build-qt-static-small-microsoft-intel-gcc-compiler to try and build the
Im doing something quite specific where I have a text input, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.