在上传文件的内容发生更改后,在IE 11中第二次尝试不会触发FileReader onload

时间:2016-05-23 05:13:11

标签: javascript internet-explorer-11 onload filereader

FileReader onload第二次没有被解雇时,当IE11仍然选择了相同的文件但文件内容发生了变化时,FireFox,Chrome一直被解雇。

操作详情

  1. On First Click,对所有浏览器都可以(但有时候IE仍然缺少文件中的一些单词)。
  2. 之后我改变了文件的内容。
  3. 然后我点击第二次尝试btnDownload,Firefox和Chrome仍在阅读更新的内容。但IE不工作!
  4. 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.
    }
    

    请帮我解决以下问题。

    1. 虽然文件内容已更改,但如何在reader.onload方法中找到该行。 alert("WANT TO GET HERE ,ALTHOUGH FILE CONTENTS ARE CHANGED.(IN IE 11)");

1 个答案:

答案 0 :(得分:1)

替换此

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.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);
});

我希望这会有所帮助。我遇到了同样的问题而且我使用了这种方法并且工作正常

工作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>

有关FileReader的更多信息,请查看此文档:Link