首页 > 解决方案 > XMLHttpRequest 事件侦听器不在 Edge 上工作

问题描述

S3Upload.prototype.uploadToS3 = function(file, url, public_url, opts) {
      var this_s3upload, type, xhr;
      this_s3upload = this;
      type = opts && opts.type || file.type;
      xhr = this.createCORSRequest('PUT', url);
      if (!xhr) {
        this.onError('CORS not supported');
      } else {
        xhr.onload = function() {
          if (xhr.status === 200) {
            this_s3upload.onProgress(100, 'Upload completed.', public_url, file);
            return this_s3upload.onFinishS3Put(public_url, file);
          } else {
            return this_s3upload.onError('Upload error: ' + xhr.status, file);
          }
        };
        xhr.onerror = function() {
          return this_s3upload.onError('XHR error.', file);
        };
        xhr.upload.onprogress = function(e) {
          var percentLoaded;
          if (e.lengthComputable) {
            percentLoaded = Math.round((e.loaded / e.total) * 100);
            return this_s3upload.onProgress(percentLoaded, (percentLoaded === 100 ? 'Finalizing.' : 'Uploading.'), public_url, file);
          }
        };
      }
      xhr.setRequestHeader('Content-Type', type);
      xhr.setRequestHeader('x-amz-acl', 'public-read');
      return xhr.send(file);
    };

我正在使用此脚本将文件上传到 S3,它是https://github.com/tadruj/s3upload-coffee-javascript的一部分。一些函数参数略有更改,但除此之外它是相同的脚本。上传本身工作正常,但是随着文件在 Edge 中上传,进度不会更新。当我上传文件时,它保持在 0%,然后在完成后跳转到 100%,中间没有任何内容。这在 Chrome 或 Firefox 中不是问题。

据我所知,问题似乎出在代码中

        xhr.upload.onprogress = function(e) {
          var percentLoaded;
          if (e.lengthComputable) {
            percentLoaded = Math.round((e.loaded / e.total) * 100);
            return this_s3upload.onProgress(percentLoaded, (percentLoaded === 100 ? 'Finalizing.' : 'Uploading.'), public_url, file);

文件上传进度的事件监听器没有触发,调试器似乎证实了这一点。这里可能是什么问题?

标签: javascriptxmlhttprequestmicrosoft-edge

解决方案


根据此https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12224510/,看起来这只是某些版本的 Edge 的问题。该问题已在 EdgeHTML 版本 18.17763 中得到修复。


推荐阅读