1

When I use progress event, I can update the progress bar for one uploading request:

function uploadFile(file) {
fileid=md5(file.name);
if {xhr[fileid] ;== undefined} {
        xhr[fileid] = new XMLHttpRequest();
        xhr[fileid].open('POST',' {% url upload %}', true);
        xhr[fileid].setRequestHeader("X-File-Name", file.name);
        xhr[fileid].setRequestHeader("X-File-id", fileid);
        xhr[fileid].upload.addEventListener('progress', onprogressHandler, false);
        xhr[fileid].upload.addEventListener('load',oncompleteHandler,false);
        xhr[fileid].send(file);
}

      function onprogressHandler(event) {
            var percent = event.loaded/event.total*100;
            var $target = $(event.target);
            console.log(uploadHolder[fileid]);
            uploadHolder[fileid].find(".upload-completed").css('width',percent+'%');
            console.log('Upload progress: ' + percent + '%');
        }

However, when I sent out more than 2 files upload requests at same time, Only the progress bar for the last file will be changed. How do I determine which file upload request the event is attached to?

Update: if I declare the fileid as local variable for uploadFile like var fileid, I cannot access fileid in the event handler. Using 'this' in the event handler give me the XMLHttpRequestUpload object.

2
  • You can look at the file details through event.currentTarget.files[n] if that's of any help? (From a previous answer). Commented Oct 13, 2011 at 21:13
  • If the above code doesn't work, it's probably because the scope of fileid isn't being managed properly, and it's being reset to the last id (e.g. in a for loop). If this is the case, your problem is outside the code you're showing - can you show the code for loading multiple files at once? Commented Oct 13, 2011 at 21:13

1 Answer 1

-1

You should look for "closure" concept for javascript. After that you'll understand the error. And this concept is so important i think, you should learn it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.