我有一个简单的文件上传angularjs服务,该服务将新的XMLHttpRequest发送到S3。我在控制器中有一个x按钮,该按钮调用一个服务方法,该服务方法将中止该上载(如果存在)。
我以前曾做过这项工作,但是由于某种原因,它停止工作到调用进度处理程序和中止处理程序的地步。是什么原因导致这种情况发生?
public uploadFile(file: File): ng.IPromise<{}> {
this.deferred = this.$q.defer();
const formData = new FormData();
...
formData.append("file", file);
const abortUpload = () => this.deferred.reject('canceled');
this.xhr = new XMLHttpRequest();
this.xhr.upload.addEventListener("progress", this.deferred.notify, false);
this.xhr.addEventListener("load", this.deferred.resolve, false);
this.xhr.addEventListener("error", this.deferred.reject, false);
this.xhr.addEventListener("abort", abortUpload, false);
this.xhr.open('POST', HOSTS.S3_UPLOAD, true);
this.xhr.setRequestHeader('Accept', '*/*');
this.xhr.send(formData);
return this.deferred.promise;
}
public cancelUploadFile(): void {
if (this.xhr) {
this.xhr.abort();
}
}
问题是我在控制器中有此
this.uploadFile(someFile).then(
() => uploadSuccess(...),
(err) => uploadError(...),
() => uploadProgress(...)
).then(<more chained promise code>);
和uploadProgress之所以被调用是因为xhr.abort()
由于某种原因从侦听器触发了deferred.notify
。在此代码中导致中止通知的原因是什么?