patterntypescriptangularCritical
Angular File Upload
Viewed 0 times
angularuploadfile
Problem
I'm a beginner with Angular, I want to know how to create Angular 5 File upload part, I'm trying to find any tutorial or doc, but I don't see anything anywhere. Any idea for this? And I tried ng4-files but it's not working for Angular 5
Solution
Here is a working example for file upload to api:
Step 1: HTML Template (file-upload.component.html)
Define simple input tag of type
Step 2: Upload Handling in TypeScript (file-upload.component.ts)
Define a default variable for selected file.
Create function which you use in
If you want to handle multifile selection, than you can iterate through this files array.
Now create file upload function by calling you file-upload.service:
Step 3: File-Upload Service (file-upload.service.ts)
By uploading a file via POST-method you should use
So, This is very simple working example, which I use everyday in my work.
Step 1: HTML Template (file-upload.component.html)
Define simple input tag of type
file. Add a function to (change)-event for handling choosing files.
Choose File
Step 2: Upload Handling in TypeScript (file-upload.component.ts)
Define a default variable for selected file.
fileToUpload: File | null = null;Create function which you use in
(change)-event of your file input tag:handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
}If you want to handle multifile selection, than you can iterate through this files array.
Now create file upload function by calling you file-upload.service:
uploadFileToActivity() {
this.fileUploadService.postFile(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}Step 3: File-Upload Service (file-upload.service.ts)
By uploading a file via POST-method you should use
FormData, because so you can add file to http request.postFile(fileToUpload: File): Observable {
const endpoint = 'your-destination-url';
const formData: FormData = new FormData();
formData.append('fileKey', fileToUpload, fileToUpload.name);
return this.httpClient
.post(endpoint, formData, { headers: yourHeadersConfig })
.map(() => { return true; })
.catch((e) => this.handleError(e));
}So, This is very simple working example, which I use everyday in my work.
Code Snippets
<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>fileToUpload: File | null = null;handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
}uploadFileToActivity() {
this.fileUploadService.postFile(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}postFile(fileToUpload: File): Observable<boolean> {
const endpoint = 'your-destination-url';
const formData: FormData = new FormData();
formData.append('fileKey', fileToUpload, fileToUpload.name);
return this.httpClient
.post(endpoint, formData, { headers: yourHeadersConfig })
.map(() => { return true; })
.catch((e) => this.handleError(e));
}Context
Stack Overflow Q#47936183, score: 662
Revisions (0)
No revisions yet.