Last active
May 11, 2022 20:57
-
-
Save bobbyg603/2602378a873ff51da87985bbaac5d7b1 to your computer and use it in GitHub Desktop.
File Uploads with Angular and Express
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
uploads$?: Observable<FileUploadProgress[]>; | |
onFilesDropped(files: NgxFileDropEntry[]): void { | |
this.uploads$ = from(files) | |
.pipe( | |
mergeMap(selectedFile => { | |
const id = uuid(); | |
const fileEntry = selectedFile.fileEntry as FileSystemFileEntry; | |
const observableFactory = bindCallback(fileEntry.file) as any; | |
const file$ = observableFactory.call(fileEntry) as Observable<File>; | |
return file$ | |
.pipe( | |
switchMap(file => this.uploadFile(file) | |
.pipe( | |
takeWhile(event => event.type !== HttpEventType.Response), | |
filter(isHttpProgressEvent), | |
map(event => { | |
const name = file.name; | |
const loaded = event.loaded ?? 0; | |
const total = event.total ?? 1; | |
const progress = Math.round(loaded / total * 100); | |
const failed = false; | |
const done = progress === 100; | |
return { | |
id, | |
name, | |
progress, | |
failed, | |
done | |
}; | |
}), | |
) | |
) | |
); | |
}), | |
scan((acc, next) => { | |
acc[next.id] = next; | |
return { | |
...acc | |
}; | |
}, {} as Record<string, FileUploadProgress>), | |
map(progress => Object.values(progress)) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment