我正在开发一个项目(BrowserIO - 如果你想查看代码并处理它,请转到 browserio dot googlecode dot com。欢迎帮助!),根据他们的示例,我正在使用 Firefox 的 nsIFileInputStream 和 nsIConverterInputStream ( https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO#Simple ),但只有一部分完整数据被加载。代码是:
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish
var str = {};
cstream.readString(-1, str); // read the whole file and put it in str.value
data = str.value;
cstream.close(); // this closes fstream
如果您想查看此行为,请从 BrowserIO 项目页面签出代码,并使用 Firebugdata = str.value;
在 file_io.js 中的行设置断点。然后从列表中选择一个文本文件,然后单击“打开”按钮。在 Firebug 中,在监视面板中为 str.value 设置监视。查看文件...它应该被截断,除非它真的很短。
作为参考,上面的代码是trunk/scripts/file_io.js中openFile()函数的主体。
有人知道这是怎么回事吗?