使用HTML5文件API我创建了一种机制,供用户从他的计算机中选择一个文件,然后将其作为字符串读取并在应用程序中处理。但是,代码在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 :(得分: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();
}
}
}