Skip to content

Commit

Permalink
Merge pull request #15668 from kubedan/master
Browse files Browse the repository at this point in the history
Fixed #15664 - Cancel upload file request
  • Loading branch information
cetincakiroglu authored Aug 15, 2024
2 parents 7dac62a + 0c0dd97 commit 38462d8
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/app/components/fileupload/fileupload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe

public uploadedFiles = [];

private fileUploadSubcription: Subscription;

constructor(
@Inject(DOCUMENT) private document: Document,
@Inject(PLATFORM_ID) private platformId: any,
Expand Down Expand Up @@ -746,7 +748,10 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe
formData.append(this.name!, this.files[i], this.files[i].name);
}

this.http
// If the previous upload hasn't been finished, it is aborted.
this.cancelUploadRequest();

this.fileUploadSubcription = this.http
.request(<string>this.method, this.url as string, {
body: formData,
headers: this.headers,
Expand Down Expand Up @@ -805,6 +810,7 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe
clear() {
this.files = [];
this.uploadedFileCount = 0;
this.cancelUploadRequest();
this.onClear.emit();
this.clearInputElement();
this.cd.markForCheck();
Expand All @@ -816,6 +822,7 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe
* @group Method
*/
remove(event: Event, index: number) {
this.cancelUploadRequest();
this.clearInputElement();
this.onRemove.emit({ originalEvent: event, file: this.files[index] });
this.files.splice(index, 1);
Expand All @@ -826,12 +833,22 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe
* @param {Number} index - Index of the file to be removed.
* @group Method
*/
removeUploadedFile(index) {
removeUploadedFile(index) {
let removedFile = this.uploadedFiles.splice(index, 1)[0];
this.uploadedFiles = [...this.uploadedFiles];
this.onRemoveUploadedFile.emit({ file: removedFile, files: this.uploadedFiles });
}

/**
* Cancel upload file request.
* */
cancelUploadRequest() {
if (this.fileUploadSubcription) {
this.fileUploadSubcription.unsubscribe();
this.fileUploadSubcription = undefined;
}
}

isFileLimitExceeded() {
const isAutoMode = this.auto;
const totalFileCount = isAutoMode ? this.files.length : this.files.length + this.uploadedFileCount;
Expand Down

0 comments on commit 38462d8

Please sign in to comment.