javascript - 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);
文件上传进度的事件监听器没有触发,调试器似乎证实了这一点。这里可能是什么问题?
解决方案
根据此https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12224510/,看起来这只是某些版本的 Edge 的问题。该问题已在 EdgeHTML 版本 18.17763 中得到修复。
推荐阅读
- css - 如何让视图以特定的间隔填充?
- c++ - 错误 LNK2001:未解析的外部符号“公共:__thiscall CDboTSTAgency::CDboTSTAgency(void)”
- payment-gateway - 如何在 Global Payments PHP API 中访问过去交易的状态?
- sql - 是否可以在 SQL 中使用数组元素作为外键?
- sql - 一次对多条记录使用更新查询
- python - TF 模型是否将 pandas.Categorical() 转换的特征视为分类?
- python - 为什么装饰器需要包装器
- javascript - svg下的图像标签中的过滤器会降低图像的质量吗?
- typescript - 打字稿:接口联盟
- reactjs - 如何确保整个应用都可以使用 Google 注销按钮