views:

33

answers:

0

When I try to read a file synchronously, Firefox freezes. When I try to read a file asynchronously, I get numbers instead of words.

This code snippet...

var MY_ID = "[email protected]";
var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "wordlist.txt");

...plus this code snippet...

var appInfo=Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
var isOnBranch = appInfo.platformVersion.indexOf("1.8") == 0;
var ios=Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var fileURI=ios.newFileURI(file);
var channel = ios.newChannelFromURI(fileURI);
var observer = {
    onStreamComplete : function(aLoader, aContext, aStatus, aLength, aResult) {
        alert(aResult);
    }
};
var sl = Components.classes["@mozilla.org/network/stream-loader;1"].createInstance(Components.interfaces.nsIStreamLoader);
if (isOnBranch) {
    sl.init(channel, observer, null);
} else {
    sl.init(observer);
    channel.asyncOpen(sl, channel);
}

...alerts numbers instead of words.

How to read a file asynchronously line-by-line?