2015-02-08 53 views
1

請幫助我使用fileReader填充爲IE9。我無法知道如何使用它。文件讀取器API填充

如果我在這裏做了任何錯誤,請糾正我。我試圖使用FileReader API Shim for IE9。但在輸入型=文件的變化時,我還在獲取文件屬性不是事件錯誤消息 規定張貼下面的代碼爲您reference`

<body> 
 
<div class="main"> 
 
    <div id="fileReaderSWFObject"></div> 
 
    <input type="file" id="imageLoader" name="imageLoader" /><br /> 
 
    <input id="text" type="text" placeholder="some text..."> 
 
</div> 
 

 
<script src="jquery-1.8.0.min.js"></script> 
 
<script src="jquery-ui-1.10.1.custom.min.js"></script> 
 
<script src="jquery.FileReader.js"></script> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script> 
 

 
<script> 
 
$(function() { 
 
// Variables 
 

 

 
// Init 
 
if ($.browser.msie && $.browser.version <= 9) { 
 
    
 
    $('#imageLoader').fileReader({ 
 
     id: 'fileReaderSWFObject', 
 
     filereader: 'filereader.swf', 
 
     expressInstall: 'expressInstall.swf', 
 
     debugMode: true, 
 
     callback: function() { console.log('filereader ready'); } 
 
    }); 
 
} 
 
$('#imageLoader').change(function (e) { 
 
    if ($.browser.msie && $.browser.version <= 9) { 
 
     console.log(e.target.files[0].name); 
 
    } 
 
}); 
 
}); 
 
</script> 
 
</body>

回答

0

最後,我才得以川方用IE9 polyfill FileReader API解決方案https://github.com/Aymkdn/FileToDataURI

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script> 
    <object id="file-object"> 
    </object> 
    <div id="file-result"></div> 
    <script type="text/javascript"> 
    var Flash = { 
     /* Flash.getFileData() is called after the file has been read */ 
     getFileData: function(base64) { 
     showResult(base64) 
     }, 
     /* getButtonLabel() permits to define another label for the "Load a file" button in the Flash version */ 
     getButtonLabel: function() { 
     return "Load a file"; 
     } 
    }; 

    // we just want to show the result into the div 
    function showResult(b) { 
     $('#file-result').text(b) 
    } 

    // check if the FileReader API exists... if not then load the Flash object 
    if (typeof FileReader !== "function") 
     // we use 80px by 23px, because we want the object to have the same size than the button 
     swfobject.embedSWF("FileToDataURI.swf", "file-object", "80px", "23px", "10", "expressInstall.swf", {}, {}, {}); 
    else { 
     // replace the <object> by an input file 
     $('#file-object').replaceWith('<input type="file" id="file-object" value="Load a file" />'); 
     $('#file-object').on('change', function(e) { 
     var files = e.target.files,file; 

     if (!files || files.length == 0) return; 
     file = files[0]; 

     var fileReader = new FileReader(); 
     fileReader.onload = function (e) { 
      // ATTENTION: to have the same result than the Flash object we need to split 
      // our result to keep only the Base64 part 
      showResult(e.target.result.split(",")[1]); 
     }; 
     fileReader.readAsDataURL(file); 
     }); 
    } 
    </script>