简体   繁体   中英

FileReader onload not getting fired for second attempt in IE 11 after contents of the uploaded file is changed

FileReader onload not getting fired on second time when the same file still selected with IE11 but the contents of file has changed, it's getting fired all the time for FireFox,Chrome.

Operation Details

  1. On First Click, its all okay for all browsers(but IE sometime still missing some words from file).
  2. After that I changed the contents of the file.
  3. And then I click second attempt to btnDownload , Firefox and Chrome still reading the updated contents. BUT IE DOES NOT WORK!

.html

<input type="file" id="fileSelect" style="" accept=".csv">

<a type="button"  class="btn" id="btnDownload" onclick="csvDownload()" 
download>  CSV DOWNLOAD  </a>

myjavascript.js

var fileInput = document.getElementById("fileSelect"); //<input type="file" id="fileSelect">
var result = "";
readFile = function() {
changeAPInfoName();
if (!isFileSupported()) {
    console.log('The browser does not support the API.');

} else {
    if (fileInput.files.length > 0) {
        var reader = new FileReader();
        reader.onload = function() {
           result = reader.result;

           alert("WANT TO GET HERE ,ALTHOUGH FILE CONTENTS ARE CHANGED.(IN IE 11)");            

           document.getElementById('MY_HIDDEN_FIELD').setAttribute('value',result);
      }

    reader.readAsText(fileInput.files[0]);

    reader.onerror = function() {
         console.log('The file cannot be read.'+fileInput.files[0].fileName);
      };
}

// EVENT FOR DOWNLOAD BUTTON!!!!
function csvDownload(){
     readFile();  

     // using ajax to sent info from files and get download file.
}

Please help me with following issue.

  1. How can I get to the line in reader.onload method although file contents are changed. alert("WANT TO GET HERE ,ALTHOUGH FILE CONTENTS ARE CHANGED.(IN IE 11)"); .

Replace this

reader.onload = function() {
       result = reader.result;

       alert("WANT TO GET HERE ,ALTHOUGH FILE CONTENTS ARE CHANGED.(IN IE 11)");            

       document.getElementById('MY_HIDDEN_FIELD').setAttribute('value',result);
  }

with

reader.addEventListener("load",function(){
result = reader.result;

           alert("WANT TO GET HERE ,ALTHOUGH FILE CONTENTS ARE CHANGED.(IN IE 11)");            

           document.getElementById('MY_HIDDEN_FIELD').setAttribute('value',result);
});

I hope this is helpful. I was facing the same problem and I used this method and it worked

Example of working FileReader:

<html>

<head>



    <script>


        function read(){
            //Select the element containing file
              var file =document.querySelector('input[type=file]').files[0];
        //create a FileReader
        reader = new FileReader();
        //add a listener
        reader.addEventListener('load',function(){

            alert(reader.result);

        },false);

        if(file){
             //ReadFile
            reader.readAsDataURL(file);//You can read it in many other forms

        }
        }

    </script>



    </head>

    <body>

    <input type="file" name="myFile" id="myFile"  onchange="read()">




    </body>

</html>

For more on FileReader check this document out : Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM