Last active
May 11, 2022 20:43
-
-
Save bobbyg603/46f00448da316c7dad5a7a898a5b3369 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
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