XHttpRequest中的进度条不起作用

时间:2017-04-28 18:30:43

标签: extjs

我有以下xHttpRequest并试图让进度条工作。 触发进度事件。但似乎只做了一次而且不再更新

uploadFile: function (file, progressBar, infoText, count) {
     var xhr = new XMLHttpRequest(),
         me = this;

     progressBar = progressBar || 0;
     infoText = infoText || null;

     xhr.open("post", me.requestURL, true);

     xhr.addEventListener('progress', function(evt) {
         // debugger;
         progressBar.update( { percent: (evt.loaded / file.size)*100 } );
     });

     // Upload complete
     xhr.addEventListener('load', function(e) {
         var target = e.target,
             response = Ext.decode(target.responseText);

         if (response.success == false && response.blacklist == true) {
             Shopware.Notification.createGrowlMessage(me.snippets.blackListTitle, Ext.String.format(me.snippets.blackListMessage, response.extension));
         }
         if (response.return == "check") {

             me.infoMessageBox(response,count);

         }

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

如果要上传文件,则必须听取xhr.uploads进度:

 xhr.upload.addEventListener('progress', function(evt) {
     progressBar.update( { percent: (evt.loaded / file.size)*100 } );
 });

或者像这样:

 xhr.upload.onprogress = function(evt) {
     progressBar.update( { percent: (evt.loaded / file.size)*100 } );
 }

最好使用evt.total代替file.size。 更多:link to MDN