I'm trying to implement file upload in my MEAN app. My backend (Node and Express) and frontend (Angular) was deployed separately. What I need is to upload the file to amazon s3 through Angular, get the address destination after successful upload then save the file destination to a variable.
Is there any easy way to implement this? Let's say I have a form like this
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<input type="file" id="myImage" (change)="onFileChange($event)" #fileInput>
<hr>
<div class="text-right">
<button type="submit" class="btn btn-primary">Actualizar perfil</button>
</div><br>
</form>
Then this code would be on my component (just for testing that I can get the file)
@ViewChild('fileInput') fileInput: ElementRef;
createForm() {
this.form = this.formBuilder.group({
'myImage': null
});
}
onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.form.get('myImage').setValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1]
})
};
}
}
onSubmit() {
const formModel = this.form.value;
this.loading = true;
setTimeout(() => {
console.log(formModel);
alert('Finished uploading');
this.loading = false;
}, 1000);
}
How would I send the file from my form to Amazon S3? Then get the file destination in amazon s3 so I can save it to my database for fetching it later. I'm also not sure If should I create the amazon s3 configuration to my backend or frontend, I'm so confused right now.
