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

  • Home
  • SEARCH
  • 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 6353631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:26:45+00:00 2026-05-24T22:26:45+00:00

I am attempting to use XMLHttpRequest to upload files to the server. Here are

  • 0

I am attempting to use XMLHttpRequest to upload files to the server. Here are the steps I would like the code to do:

  1. List the file in a file list ul
  2. Upload the file(s) while displaying a progress bar
  3. When the file is completely uploaded, change the list item for that file to a link without the progress bar. This is where I am having a problem.

Here is the HTML code I am working with:

<h1>Upload Files</h1>
<input type='file' name='doc_upload_field[][]' multiple='multiple' id='doc_upload_field' /> 
<ul id='filelist'></ul>

Here is the javascript code I am working with:

function transferFailed(evt) {
    alert("An error occurred while transferring the file.");
}                       
function transferCanceled(evt) {
    alert("The transfer has been canceled by the user.");
}

var filelist = $('#filelist');//define where the list of files will go
var url = "/";

function handleFileSelect_inputfield(evt) {
  var files = evt.target.files; // FileList object


  // run through each file individually.
  for (var i = 0, f; f = files[i]; i++) {    

  var li = $("<li><strong>" + f.name + "</strong> (" + f.type  +") - " + f.size + " bytes <div class='progress_bar'><div class='percent'>&nbsp;</div></div></li>");
  filelist.append(li);//put the file in the filelist

  var formData = new FormData(); 
  formData.append(f.name, f);

  //upload through xmlhttprequest
  var xhr = new XMLHttpRequest();           
  xhr.upload.li = li;
  xhr.upload.addEventListener("progress", function(e) {
    if (e.lengthComputable) {
      var percentLoaded = Math.round((e.loaded / e.total) * 100);                     
      // Increase the progress bar length.
      if (percentLoaded < 100) {
        this.li.find(".progress_bar").addClass('loading');
        this.li.find(".percent").width(percentLoaded + '%');
      } 
    }               
  }, false);
  xhr.upload.addEventListener("load", function(e) {
    //uploading is complete                 
  }, false);        

  xhr.upload.addEventListener("error", transferFailed, false);
  xhr.upload.addEventListener("abort", transferCanceled, false);

  xhr.open('POST', url, true);
  xhr.responseType = 'text';
  xhr.onload = function(e) {                
    if (this.readyState==4){  
      if (this.status == 200) { 
          //console.log(this.response);
          console.log("finished=" + li);//returns as an object
          //console.log("filename=" + f.name);//error: cannot read property 'name' of undefined              
          //change the list item. Not working...
          this.li.find("li").html("<a href='#'>" + f.name + "</a>");                      
      }
    }
  };                                    
  xhr.send(formData);  // multipart/form-data                     
  }     
}

document.getElementById('doc_upload_field').addEventListener('change', handleFileSelect_inputfield, false);

Everything seems to be working, but when I want to change the list item, li, it is not recognizing it. I am able to change the progress bar length by calling:

this.li.find(".progress_bar").addClass('loading');
this.li.find(".percent").width(percentLoaded + '%');

But when I want to change the list item:

this.li.find("li").html("<a href='#'>" + f.name + "</a>");

It does not work.

Does anyone know why it would find it when changing the progress bar, but not find it after it is uploaded?

  • 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-24T22:26:46+00:00Added an answer on May 24, 2026 at 10:26 pm

    The expression

    this.li.find("li")
    

    looks for an <li> element inside the other <li> element. There isn’t one, so nothing happens.

    I think that just

    this.li.html("<a href='#'>" + f.name + "</a>");
    

    should be what you want. Or you could just do this:

    this.li = $("<li><a href='#'>" + f.name + "</a></li>");
    

    edit oops no that won’t work because you’ve stuck it in the DOM already. But the first one should work.

    edit — you’ve also got a closure-related problem (or scope-related; whatever). What you can do is something like what you’ve already done for the “li” value. Add another property for it to “xhr.upload”:

    xhr.upload.updated_li = "<a href='#'>" + f.name + "</a>";
    

    Do that right where you set “xhr.updated.li”. Then, in the event handler, you can do:

      this.li.html(this.updated_li);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use MIB files in PySNMP. The code is fairly straightforward. Nothing
Attempting to use asp.net mvc's Action Result of File. So it would seem that
When attempting to use HttpWebRequest to retrieve a page from my dev server, I
I am attempting to use the 'tempfile' module for manipulating and creating text files.
We are attempting to use a SQL Server 2003 database for our test records
When attempting to use pandoc to convert JSON based files (.ipynb) from iPython notebook
I am attempting to use Castle Windsor in my automated tests like so: On
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
I am attempting to use Inno Setup to bootstrap a WiX generated .msi file
I'm attempting to use the following code to serialize an anonymous type to JSON:

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.