2011-08-10 24 views
2

使用HTML5文件API我已經創建了一種機制,供用戶從他的計算機中選擇一個文件,然後將其讀取爲字符串並在應用程序中處理。但是,該代碼在IE9中不起作用,所以我正在尋找一種解決方案。這是我的代碼,它創建了一個文件讀者對象:在IE9中以字符串形式讀取用戶指定的文件

function CreateFileReader(element) 
{ 
    var self=this; 

    // create an input field and insert it into the document  
    this.element=element; 
    this.element.html(''); 
    var fileBox=$('<input type="file"/>'); 
    this.element.append(fileBox); 

    // when the contents (file) of the fileBox change, read the file  
    this.fileBox.change(function() { 
     if (this.files.length > 0){ 
      if (this.files[0]!=undefined) { 
       var file=this.files[0]; 
       // set up the file reader 
       var reader = new FileReader(); 
       reader.file=file; 
       // specify what happens when the file is loaded 
       reader.onloadend = self.processFile; 

       // read the file as a text string 
       reader.readAsText(file); 
      } 
     } 
    }); 
} 

CreateFileReader.prototype.processFile = function(e) { 
    // if the file was loaded successfully 
    if (e.target.error==null && e.target.readyState==2) { 
     var fileString=e.target.result; 
     // do some stuff with fileString here 
    } 
} 

我會很感激,如果你能表明,IE9的工作方案。

+0

相關:http://stackoverflow.com/questions/5397991/html-4-equivalent-of-html-5s-filereader和http ://stackoverflow.com/questions/5055724/flash-alternative-for-filereader-html-5-api – HoLyVieR

回答

1

您可以使用silverlight後備。

的Javascript:

if (!window.FileReader) 
    { 
     content.innerHTML = 
      '<div style="width: 20em; height: 3em;">' + 
      '<object id="silverlightFallback" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">' + 
      ' <param name="source" value="ClientBin/SilverlightFallback.xap"/>' + 
      ' <param name="background" value="white" />' + 
      ' <param name="minRuntimeVersion" value="4.0.50826.0" />' + 
      ' <param name="autoUpgrade" value="true" />' + 
      ' <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">' + 
      '  <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>' + 
      ' </a>' + 
      '</object>' + 
      '</div>'; 

     var script = document.createElement("script"); 
     var silverlightFallback = document.getElementById("silverlightFallback"); 

     silverlightFallback.OnLoad = function() { 
      silverlightFallback.content.FileReader.RegisterCallback(function (arr) { alert(arr); }); 
     }; 

     script.setAttribute("type", "text/javascript"); 
     script.setAttribute("src", "Silverlight.js"); 

     document.getElementsByTagName("head")[0].appendChild(script); 
    } 

的Silverlight:

public partial class MainPage : UserControl 
{ 
    private FileReaderFallback _reader = new FileReaderFallback(); 

    public MainPage() 
    { 
     InitializeComponent(); 

     HtmlPage.RegisterScriptableObject("FileReader", _reader); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     _reader.OpenFile(); 
    } 
} 

[ScriptableType] 
public class FileReaderFallback 
{ 
    private ScriptObject _callback; 

    [ScriptableMember] 
    public void RegisterCallback(ScriptObject callback) 
    { 
     _callback = callback; 
    } 

    public void OpenFile() 
    { 
     var dialog = new OpenFileDialog() { Multiselect = false }; 
     var result = dialog.ShowDialog(); 

     if (result == true) 
     { 
      ScriptObject arr = HtmlPage.Window.CreateInstance("Array"); 

      using (var s = dialog.File.OpenRead()) 
      { 
       byte[] buffer = new byte[1024]; 
       int count; 

       while ((count = s.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        Array.ForEach(buffer, b => arr.Invoke("push", (int)b)); 
       } 
      } 

      if (_callback != null) 
       _callback.InvokeSelf(arr); 
     } 
     else 
     { 
      if (_callback != null) 
       _callback.InvokeSelf(); 
     } 
    } 
} 
相關問題