Uploading Files & Progress Events
Upload files with progress bars and download files with blob responses. FormData + reportProgress = complete file handling.
Uploading files in Angular uses FormData โ the browser's native API for multipart form data.
// Basic upload: FormData + POST
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('name', 'Hyderabadi Biryani');
this.http.post('/api/upload', formData).subscribe(response => {
console.log('Upload complete:', response);
});
"File upload = parcel bhejna โ properly pack karo, phir bhejo."
FormDatawraps files and text fields into multipart form data- POST the FormData to the server endpoint
- HttpClient automatically sets
Content-Type: multipart/form-datawith the correct boundary - Server must accept
multipart/form-dataโ most backends do
FormData is the browser API for creating multipart requests. You append files and text fields.
// HTML
<input type="file" (change)="onFileSelect($event)" multiple accept="image/*" />
// TypeScript
onFileSelect(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
if (file) {
this.selectedFile = file;
this.fileName = file.name;
this.fileSize = (file.size / 1024).toFixed(2) + ' KB';
}
}
// Create FormData
const formData = new FormData();
formData.append('file', this.selectedFile);
formData.append('name', 'Hyderabadi Biryani');
formData.append('category', 'chicken');
"FormData = packing box โ file daalo, naam daalo, extra info daalo."
Common FormData methods:
append(name, value)โ add a field or filedelete(name)โ remove a fieldget(name)โ get a field's valuehas(name)โ check if field existsset(name, value)โ replace a field (like append but overwrites)
For multiple files, append the same name multiple times:
for (const file of files) {
formData.append('photos', file);
}The simplest upload โ just FormData + POST.
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-upload',
standalone: true,
imports: [FormsModule],
template: `
<div class="upload-container">
<h2>Upload Biryani Photo</h2>
<input type="file" (change)="onFileSelect($event)" accept="image/*" />
<div *ngIf="selectedFile" class="file-info">
<p>๐ {{ selectedFile.name }}</p>
<p>Size: {{ (selectedFile.size / 1024).toFixed(1) }} KB</p>
</div>
<button (click)="upload()" [disabled]="!selectedFile || uploading">
{{ uploading ? 'Uploading...' : '๐ค Upload' }}
</button>
</div>
`
})
export class UploadComponent {
private http = inject(HttpClient);
selectedFile: File | null = null;
uploading = false;
onFileSelect(event: Event) {
const input = event.target as HTMLInputElement;
this.selectedFile = input.files?.[0] || null;
}
upload() {
if (!this.selectedFile) return;
this.uploading = true;
const formData = new FormData();
formData.append('file', this.selectedFile);
formData.append('name', 'Biryani Photo');
this.http.post('/api/upload', formData).subscribe({
next: (response) => {
console.log('Upload successful:', response);
this.uploading = false;
},
error: (error) => {
console.error('Upload failed:', error);
this.uploading = false;
}
});
}
}
"Itna simple hai โ FormData banao, POST karo, bas."
That's really it. FormData + POST = file upload. Everything else (progress, preview, validation) is extra polish on top.
Track upload progress with reportProgress: true and observe: 'events'.
import { HttpEventType, HttpEvent } from '@angular/common/http';
import { filter, map } from 'rxjs/operators';
this.http.post('/api/upload', formData, {
reportProgress: true, // โ Enable progress events
observe: 'events' // โ Get all events, not just final response
}).pipe(
filter(event => event.type === HttpEventType.UploadProgress),
map(event => ({
progress: Math.round((event.loaded / event.total!) * 100),
loaded: event.loaded,
total: event.total
}))
).subscribe(progress => {
this.uploadProgress = progress.progress; // 0 to 100
this.loadedBytes = progress.loaded;
this.totalBytes = progress.total;
});
"Progress = Swiggy tracking โ kitna percent hua dikhata hai."
Key options:
reportProgress: trueโ emits progress events during upload/downloadobserve: 'events'โ returns ALL events (sent, upload progress, response)HttpEventType.UploadProgressโ filter for progress events onlyevent.loaded / event.total * 100โ calculate percentage
Progress bar in template:
<div class="progress-bar">
<div class="progress-fill" [style.width.%]="uploadProgress"></div>
</div>
<p>{{ uploadProgress }}% uploaded</p>Downloading files uses responseType: 'blob' to receive binary data.
downloadFile() {
this.http.get('/api/export/biryanis', {
responseType: 'blob' // โ Expect binary data, not JSON
}).subscribe(blob => {
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a temporary anchor to trigger download
const a = document.createElement('a');
a.href = url;
a.download = 'biryani-list.csv'; // Suggested filename
a.click();
// Clean up the blob URL to free memory
URL.revokeObjectURL(url);
});
}
"Download = reverse upload โ server se file laake browser mein save karo."
Key points:
responseType: 'blob'โ tells Angular not to parse as JSON, keep as binaryURL.createObjectURL(blob)โ creates a temporary URL pointing to the blob in memorydocument.createElement('a')+.click()โ triggers browser downloadURL.revokeObjectURL(url)โ CRITICAL: releases memory. Blob URLs persist until revoked.
For Excel/PDF downloads, the same pattern works โ just set the filename extension correctly.
Key Takeaways
- โ File upload: FormData + POST โ HttpClient auto-sets multipart/form-data with boundary
- โ FormData.append(fileName, file) for files, FormData.append(key, value) for text fields
- โ reportProgress: true + observe: 'events' + filter(HttpEventType.UploadProgress) for progress
- โ Download: responseType: 'blob' + URL.createObjectURL() + anchor.click() + URL.revokeObjectURL()
- โ NEVER manually set Content-Type for FormData โ HttpClient handles the boundary automatically
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login