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);
})
}
<input type="file" class="file-upload" (change)="functionName($event)">
onFileSelected(event) {
if(event.target.files.length > 0)
{
this.myForm.patchValue({
fileName: event.target.files[0],
})
}
}
submit(){
const formData = new FormData();
formData.append('file', this.myForm.get('fileName').value);
this.http.post('http://localhost:3000/upload', formData)
.subscribe(res => {
console.log(res);
alert('Uploaded Successfully.');
})
}
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();
};
}
}
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);
})
}