Skip to content

Instantly share code, notes, and snippets.

@bobbyg603
Last active May 11, 2022 20:43
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/46f00448da316c7dad5a7a898a5b3369 to your computer and use it in GitHub Desktop.
Save bobbyg603/46f00448da316c7dad5a7a898a5b3369 to your computer and use it in GitHub Desktop.
File Uploads with Angular and Express
onFilesDropped(files: NgxFileDropEntry[]): void {
from(files)
.pipe(
mergeMap(selectedFile => {
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))
);
})
).subscribe(console.log);
}
private uploadFile(file: File): Observable<HttpEvent<unknown>> {
const url = 'http://localhost:8080/upload';
const formData: FormData = new FormData();
formData.append('file', file, file.name);
return this.httpClient.post(
url,
formData,
{
reportProgress: true,
observe: 'events'
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment