Skip to content

Instantly share code, notes, and snippets.

@bobbyg603
Last active May 11, 2022 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbyg603/2602378a873ff51da87985bbaac5d7b1 to your computer and use it in GitHub Desktop.
Save bobbyg603/2602378a873ff51da87985bbaac5d7b1 to your computer and use it in GitHub Desktop.
File Uploads with Angular and Express
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