This document provides code examples for implementing file upload functionality in Angular applications with a Web API backend, covering different Angular versions.
<input type="file" (change)="onFileSelected($event)" multiple="">
<button (click)="uploadFiles()">Upload</button>
<progress [value]="uploadProgress" max="100"></progress>
@Component({ standalone: true, imports: [CommonModule] })
export class FileUploadComponent {
private http = inject(HttpClient);
selectedFiles: File[] = [];
uploadProgress = 0;
onFileSelected(event: Event) {
const input = event.target as HTMLInputElement;
this.selectedFiles = input.files ? Array.from(input.files) : [];
}
uploadFiles() {
const formData = new FormData();
this.selectedFiles.forEach(file => formData.append('files', file));
this.http.post('/api/upload', formData, {
reportProgress: true,
observe: 'events'
}).subscribe({
next: (event) => {
if (event.type === HttpEventType.UploadProgress && event.total) {
this.uploadProgress = Math.round((100 * event.loaded) / event.total);
}
},
error: (err) => console.error(err)
});
}
}
@Component({ /* NgModule-based */ })
export class FileUploadComponent {
constructor(private http: HttpClient) {}
selectedFiles: File[] = [];
onFileSelected(event: any) {
this.selectedFiles = Array.from(event.target.files);
}
uploadFiles() {
const formData = new FormData();
this.selectedFiles.forEach(file => formData.append('files', file));
this.http.post('/api/upload', formData, {
reportProgress: true,
observe: 'events'
}).subscribe({
next: (event) => { /* Same as Angular 17 */ },
error: (err) => console.error(err)
});
}
}
@Component({ /* NgModule-based */ })
export class FileUploadComponent {
constructor(private http: HttpClient) {}
selectedFiles: FileList | null = null;
onFileSelected(event: any) {
this.selectedFiles = event.target.files;
}
uploadFiles() {
if (!this.selectedFiles) return;
const formData = new FormData();
Array.from(this.selectedFiles).forEach(file => formData.append('files', file));
this.http.post('/api/upload', formData, {
reportProgress: true,
observe: 'events'
}).subscribe(
(event) => { /* Same progress handling */ },
(err) => console.error(err)
);
}
}
@Component({ /* NgModule-based */ })
export class FileUploadComponent {
constructor(private http: HttpClient) {}
selectedFiles: FileList | null = null;
onFileSelected(event: any) {
this.selectedFiles = event.target.files;
}
uploadFiles() {
if (!this.selectedFiles) return;
const formData = new FormData();
for (let i = 0; i < this.selectedFiles.length; i++) {
formData.append('files', this.selectedFiles[i]);
}
this.http.post('/api/upload', formData, {
reportProgress: true,
observe: 'events'
}).subscribe(
(event) => { /* Same progress handling */ },
(err) => console.error(err)
);
}
}
// Component definition
@Component({ standalone: true, imports: [CommonModule] })
// File selection (strict typing)
onFileSelected(event: Event) {
const input = event.target as HTMLInputElement;
this.selectedFiles = input.files ? Array.from(input.files) : [];
}
// Uses constructor DI (like older versions)
constructor(private http: HttpClient) {}
// FileList conversion (explicit null checks)
if (!event.target.files) return;
this.selectedFiles = Array.from(event.target.files);
// Mandatory null checks
selectedFiles: FileList | null = null;
// Safer event handling
if (event.type === HttpEventType.UploadProgress && event.total) {
// TypeScript knows `event.total` exists
}
// Legacy FileList handling
selectedFiles: FileList; // No strict null checks
// Manual type assertions for HTTP progress
if (event.type === HttpEventType.UploadProgress) {
const progress = (event.loaded / (event as any).total) * 100;
}
Work with our skilled Angular developers to accelerate your project and boost its performance.
3rd Floor, Aval Complex, University Road, above Balaji Super Market, Panchayat Nagar Chowk, Indira Circle, Rajkot, Gujarat 360005.
Abbotsford, BC
15th B Street 103, al Otaiba Dubai DU 00000, United Arab Emirates
3rd Floor, Aval Complex, University Road, above Balaji Super Market, Panchayat Nagar Chowk, Indira Circle, Rajkot, Gujarat 360005.
Abbotsford, BC
15th B Street 103, al Otaiba Dubai DU 00000, United Arab Emirates
Copyright © 2026 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.