upload files with angular
fileChange(element) { this.uploadedFiles = element.target.files; } upload() { let formData = new FormData(); for (var i = 0; i < this.uploadedFiles.length; i++) { formData.append("uploads[]", this.uploadedFiles[i], this.uploadedFiles[i].name); } this.http.post('/api/upload', formData) .subscribe((response) => { console.log('response received is ', response); }) }
file upload in angular 10
onFileChange(event) { const reader = new FileReader(); if (event.target.files && event.target.files.length) { const [file] = event.target.files; reader.readAsDataURL(file); reader.onload = () => { this.data.parentForm.patchValue({ tso: reader.result }); // need to run CD since file load runs outside of zone this.cd.markForCheck(); }; } }
Source: stackoverflow.com