1

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?

  • 1
    Try adding this tag to the head of your page: – MUlferts Jan 30 '19 at 20:51
  • Tried it, did not seem to change result – Namig Ismayilov Jan 30 '19 at 20:54
  • 1
    It looks like the unbind() method is deprecated, maybe causing IE issues? Try using the off() method instead. [W3school Source](https://www.w3schools.com/jquery/event_unbind.asp]) – MUlferts Jan 30 '19 at 21:01
  • Just tried it, looks like it is acting the same – Namig Ismayilov Jan 30 '19 at 21:09
  • Are there any errors in the console log when something does not work right? – MUlferts Jan 30 '19 at 21:12
  • Console is clean, no errors – Namig Ismayilov Jan 30 '19 at 21:17
  • Why are you using the extra javascript code instead of just handling the files with the html file input? Are you just rtying to view the filenames included? – MUlferts Jan 30 '19 at 21:28
  • 1
    I did a test with sample code and I am able to produce the issue with .unbind() and .off(). I will try to provide the feedback regarding this issue to MS Edge developers. If it is not necessary for you to use this function than you can try to use the workaround suggested by the community member. Thanks for your understanding. – Deepak-MSFT Jan 31 '19 at 03:17

1 Answers1

1

I am still not sure what your intentions were with using the onclick for handling files, but I would suggest letting the html file input handle everything to do with the files. If you just want to be able to print out all the file names attached, then you could use a separate function for that. Let me know if I am understanding what you are trying to do with the example below.

$(function(){
  $('#myFile').change(function() {
    var files = $(this)[0].files;
    var addList = "";
    for (i = 0; i < files.length; i++){
      addList += "<p>" + files[i].name + "</p>";
    }
    $('#fileSummary').html(addList);
  });

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form action="/home/uploadfiles" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="myFile" multiple />
    <br>
    <input type="submit" name="submit" value="Submit" />
</form>

<hr>
<div>
  <label>Files Inlcuded:</label>
  <br>
  <span id="fileSummary"></span>
</div>

Edit. Here is a sample using the "hack" to remotely use a file input. This is not normal behavior as is mentioned in the post I linked, but you can see how this will work to remotely use a file input:

HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="remoteClick">Remote File use</button>

<br>
<hr>

<div class="hiddenfile">
  <input name="upload" type="file" id="fileinput"/>
</div>

Javascript:

$(function(){
  $('#remoteClick').click(function(){
    $('#fileinput').trigger('click'); 
  }); 
});

CSS:

.hiddenfile {
 width: 0px;
 height: 0px;
 overflow: hidden;
}
MUlferts
  • 1,310
  • 2
  • 16
  • 30
  • The reason I am using onclick is, I have a toolbar that has 'include attachment' icon. I also do not want the input file to be visible on a page. So I created a button that triggers file browser when clicked and also did display hidden on the input file. – Namig Ismayilov Jan 31 '19 at 15:38
  • Oh ok, I understand. Maybe you can try to use the default file input behavior still by using one of the alternate options in [this question](https://stackoverflow.com/questions/793014/jquery-trigger-file-input). Look at the 2 most upvoted answers. They implement the default file input behavior remotely like it sounds you are trying to do. I will edit my answer with the code that you could use. – MUlferts Jan 31 '19 at 16:18