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 6577111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:35:42+00:00 2026-05-25T15:35:42+00:00

I am getting the wrong filename in the following code. I’m sure its obvious

  • 0

I am getting the wrong filename in the following code. I’m sure its obvious what the mistake is but i dont see it. I get the correct amount of errors however all the filename are the same rather then the filename in error.

for(var i=0; i<this.files.length; ++i){
    var f = this.files[i];
    var reader = new FileReader();
    reader.onload = function(e) {
        if(e.total>maxFilesize){
            tooBigFilesLs.push(f.name);
  • 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-05-25T15:35:43+00:00Added an answer on May 25, 2026 at 3:35 pm

    Javascript Closures

    There is just an example about it in that article in “Creating closures in loops: A common mistake”

    Try with this:

    for(var i=0; i<this.files.length; ++i){
        var f = this.files[i];
        var reader = new FileReader();
    
        (function(name){
    
            reader.onload = function(e) {
                if(e.total>maxFilesize){
                        tooBigFilesLs.push(name);
            }
        }
         }(f.name));
    }
    

    (not tested)

    Basically, when you define that function on ‘onload’, you are creating a closure, and all the functions you create in that loop share it, so all the functions has access to ‘f’ and ‘reader’, so ‘f’ will remain pointing to the last file in the loop. Adding the anonymous closure in my example, using that ‘name’ parameter, you ensure that the function in the ‘onload’ gets the right name.

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

Sidebar

Related Questions

Hey! Maybe I am getting the idea of a subclass wrong, but I have
I'm trying to create a rails app, but somethings going wrong, I'm getting a
What is wrong with strcpy() in this code? void process_filedata(char *filename) { void* content;
hi for the following code, why is it that i am getting this output
In the following code, I'm having some sort of issue getting my .yaml file
What's wrong with this Groovy code? class Pretending { public int count=0 Object get(String
I wrote a code to read files What is wrong in the following code
What am I doing wrong in the following code? public string ReadFromFile(string text) {
I am getting a Wrong type argument: commandp, (lambda nil (forward-line 5)) here. (global-set-key
I am getting a wrong output when i alert this, it shows '01' <input

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.