Well, it is based on javascript approach of handling the upload, so in angular2, I upload the image in the following way using typescript..Th input type file is the key here.
The src is binded by a property whose value is set inside the code with the returned aws s3 url provided by the server response, when I send him the image url to save it.
The html :-
<div class="mt-10 fileUpload">
<label class="uploadBtnLabel bm-action-btn">
<input type="file" name="resourceFile" #uploadElem accept="image/*" (change)="uploadImage(submitElem)"/>
<input type = "submit" id="fileUpload" class="hidden" name="fileUpload" #submitElem (click)="store.postFileUpload(uploadElem, group)">
<span>Upload<i class=" pl-5 fa fa-upload"></i></span>
</label>
</div>
<img class="logoImage" [src]="group.controls['logoUrl'].value" *ngIf="group.controls['logoUrl'].value">
The typescript functions for the same to convert the image into dataUrl and send it to the server.
//Function to upload local file
uploadImage(submitElem, renderer){
let event = new MouseEvent('click', {bubbles: false});
renderer.invokeElementMethod(
submitElem, 'dispatchEvent', [event]);
}
postFileUpload(inputValue, group){
this.readImage(inputValue, group);
}
readImage(inputValue: any, group): void {
if(inputValue) {
let image;
let self = this;
let accountId;
var file: File = inputValue.files[0];
var myReader: FileReader = new FileReader();
myReader.readAsDataURL(file)
myReader.onloadend = (e) => {
image = myReader.result.split(',')[1];
let postData = {
imageString: image
};
if(group.controls['accountId']) {
accountId = group.controls['accountId'].value;
}else{
accountId = self.mssId;
}
self.imageUploadSub = self.invoiceConfService.uploadImage(accountId, postData).subscribe(
(response) => {
group.controls['logoUrl'].setValue(response.data.logoUrl);
self.imageLink = response.data.logoUrl;
},
(error) => {
});
}
}
}