I have button that triggers input file and opens file browser to select file. Once the file is selected I am running certain operations.
Problem:
It works fine on Chrome but Microsoft Edge has inconsistent behavior.In my experience first few tries(1,2 maybe 3) it does not go to this function below but it works fine after that.
$(":file").unbind().change(function () {console.log("file selected");
...
...
}
The code down below is exactly what I have used to test it against Chrome and Microsoft Edge
function toolbar_click(){
var elem = document.getElementById("myFile");
var count = 0;
if (elem && document.createEvent) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
// dataSource.add({ ProductName: "New Product" });
$(":file").unbind().change(function () {
console.log("file selected");
if (count < 1) {
if (this.files && this.files[0]) {
var reader = new FileReader();
//reader.onload = editViewModel.addFile();
reader.readAsDataURL(this.files[0]);
//$('#showfilenames').append(this.files[0].name + '</br>');
}
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="myFile" style="display:none" >
</body>
<button class="k-button" id="insertAttachment" onclick="toolbar_click()" > Click to choose file </button>
Any ideas how to fix it?