Last active
April 24, 2022 17:19
-
-
Save senoritadeveloper01/47007573c0a39af46ab0597e740eb28b to your computer and use it in GitHub Desktop.
File Upload Process Component Methods (Angular)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
startUpload(): void { | |
const filesToUpload = []; | |
this.files.forEach(f => { | |
const uuid = UtilService.generateUUID(); | |
filesToUpload.push({ | |
'name': f.name, | |
'size': f.size, | |
'uuid': uuid | |
}); | |
this.fileUUIDList.push(uuid); | |
}); | |
this.sharingSubscription = from(this.getSharingLink(filesToUpload)).subscribe(shareResponse => { | |
if (!shareResponse) { | |
return; | |
} | |
this.uploadSubscription = from(this.getUploadLinks()).subscribe((uploadResponse) => { | |
if (!uploadResponse) { | |
return; | |
} | |
const multipleFileUploadRequests = []; | |
this.files.forEach((f, index) => { | |
const fileInfo = { | |
file: f, | |
signature: this.uploadLinkInfo[index].uploadUUID, | |
url: this.uploadLinkInfo[index].uploadLink | |
}; | |
multipleFileUploadRequests.push(this.uploadFile(fileInfo)); | |
}); | |
this.forkJoinSubscription = forkJoin(multipleFileUploadRequests).subscribe( | |
() => { | |
this.progressSize = this.finalSize; | |
this.transferredSize = 100; | |
this.isUploadComplete = true; | |
}, | |
error => { | |
this.isUploadFailure = true; | |
this.notificationService.showError(this.translateService.instant('ERR_UNKNOWN_ERROR_HAS_OCCURED')); | |
}); | |
}); | |
}); | |
} | |
uploadFile(fileInfo: any): Observable<any> { | |
return this.uploadService.uploadFile(fileInfo).pipe(map( | |
event => { | |
if (this.utilService.isHttpProgressEvent(event)) { | |
this.uploadingFileMap.set(fileInfo.file.name, event.total ? event.loaded : 0); | |
let sum = 0; | |
this.uploadingFileMap.forEach((v) => sum += v); | |
this.transferredSize = sum; | |
this.progressSize = Math.round(this.transferredSize * 100 / this.finalSize); | |
this.transferredSizeStr = UtilService.formatBytes(Number(this.transferredSize)); | |
} | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment