Skip to content

Instantly share code, notes, and snippets.

@sunnyy02
Created September 11, 2022 00:14
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 sunnyy02/9d3e47c3e8274469a64a105afa0a1478 to your computer and use it in GitHub Desktop.
Save sunnyy02/9d3e47c3e8274469a64a105afa0a1478 to your computer and use it in GitHub Desktop.
attachFile(event: any) {
const file: File = event.target.files?.[0];
if (file && this.isValid(file)) {
this.errorMessage = '';
const formData = new FormData();
formData.append('file', file, file.name);
const uploadFileHeaders = new HttpHeaders({
Accept: `application/json, text/plain, */*`,
});
this.httpClient
.post('/api/file-upload/upload', formData, {
headers: uploadFileHeaders,
})
.subscribe({
next: (response) => {
this.successMessage = `Document ${file.name} is uploaded successfully`;
},
error: (error) => {
this.errorMessage = `failed to upload document.`;
return error;
},
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment