I know how to do amazon s3 file upload in angular 1. May i know how to upload file in angular 2. but i didn't find any solution for angular 2.
It still needs better/some security.
<form (ngSubmit)="onSubmit(f)" #f="ngForm" action="">
<input type="file" (change)="fileEvent($event)" />
</form>
<button (click)="uploadfile(f)">Upload file!</button>
export class Page2 {
myfile:any;
file:any;
constructor() {
}
uploadfile(event) {
AWS.config.accessKeyId = 'YOUR-ACCESS-KEY';
AWS.config.secretAccessKey = 'YOU-SECRET-ACCESS-KEY';
var bucket = new AWS.S3({params: {Bucket: 'YOUR-BUCKET-NAME'}});
var params = {Key: this.file.name, Body: this.file};
bucket.upload(params, function (err, data) {
console.log(err, data);
});
}
fileEvent(fileInput: any){
var files = event.target.files;
var file = files[0];
this.file = file;
}
}
If you are using a Rails backend (or even Node) the easiest solution is to create a presigned url (which must be a PUT) and then use an Angular service to call that PUT and attach the file in the body of the request.
You can read more about how I did that here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With