[英]Reading a user-specified file as a string in IE9
使用 HTML5 文件 API 我为用户创建了一种机制,可以从他的计算机中读取 select 文件,然后将其作为字符串读取并在应用程序中处理。 但是,该代码在 IE9 中不起作用,所以我正在寻找一个可行的解决方案。 这是我的代码,它创建了一个文件阅读器 object:
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 中工作的替代方案,我将不胜感激。
您可以使用 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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.