XMLHttpRequest事件侦听器在Edge上不起作用

时间:2019-06-12 20:23:47

标签: javascript xmlhttprequest microsoft-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);

用于文件上传进度的事件侦听器未触发,调试器似乎确认了这一点。这里可能是什么问题?

1 个答案:

答案 0 :(得分:0)

根据https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12224510/,这似乎只是某些版本的Edge的问题。此问题已在EdgeHTML版本18.17763中得到解决。

相关问题