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.

Eines He
  • 187
  • 1
  • 2
  • 8
  • You can look at the file details through `event.currentTarget.files[n]` if that's of any help? ([From a previous answer](http://stackoverflow.com/questions/7719264/html5-js-display-multiple-file-input-in-div/7719334#7719334)). – David Thomas Oct 13 '11 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? – nrabinowitz Oct 13 '11 at 21:13

1 Answers1

-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 :)