I am using Angular 5 with Typescript. I need to open the file explorer window to add an attachment on clicking an icon. Now, i know how to do this for a button but somehow it does not seem to be working for icon, maybe the click event binding is not working. A little help please?
<input type="file" #file (change)="upload()"/>
<span class="icon-doc" (click)="file.click()">
</span>
And in my component :
upload(){
//The functionality to upload file(s)
}
I am not sure how exactly your code is written, but you will need to bind that icon to a click method, which will actually programatically click the other input element that handles the attaching of files. This is one way you can do it:
<a (click)="handleClick()" href="javascript:undefined">
<i class="la la-upload"></i>
</a>
<input class="hidden" type="file" id="upload-file" name="upload-file" accept=".csv" ngf-max-size="2MB" (change)="addAttachment($event)">
You will probably want to hide the input button using CSS:
.hidden {
visibility: hidden;
width: 1px;
height: 1px;
}
And on your component.ts,
handleClick() {
document.getElementById('upload-file').click();
}
addAttachment(fileInput: any) {
const fileReaded = fileInput.target.files[0];
// handle the rest
}
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