2

I spent 4 hours trying to find a solution for loading a file into my Firefox Add-on. But, with no success (((.

The code I have:

const {TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
var decoder = new TextDecoder();
var promise = OS.File.read("C:\\test.txt");
promise = promise.then(function onSuccess(array) {
    alert(decoder.decode(array));
});

It's impossible to force the code above to run (((. What am I doing wrong?

4

1 回答 1

0

The code you have basically works as written. However, that assumes that alert() is defined and there was no error when you tried to read the file. However, alert() will not normally be defined, unless you have defined it somewhere else in your code. What exactly the issue is would likely be determined if you looked in the Browser Console (Ctrl-Shift-J, or Cmd-Shift-J on OSX). Unfortunately, you have not included that information in the question.

Reference information:

Below is a complete Firefox Add-on SDK extension which twice reads and outputs to the console the file at B:\testFile.txt. The output in the console for the example text file included below is:

read-text-file:readTextFile: This is a text file line 1
Line 2
read-text-file:readUtf8File: This is a text file line 1
Line 2

index.js:

var {Cu} = require("chrome");
//Open the Browser Console (Used in testing/developmenti to monitor errors/console)
var utils = require('sdk/window/utils');
activeWin = utils.getMostRecentBrowserWindow();
activeWin.document.getElementById('menu_browserConsole').doCommand();
var buttons     = require('sdk/ui/button/action');
var button = buttons.ActionButton({
    id: "doAction",
    label: "Do Action",
    icon: "./myIcon.png",
    onClick: doAction
});
//Above this line is specific to the Firefox Add-on SDK
//Below this line will also work for Overlay/XUL and bootstrap/restartless add-ons
//const Cu = Components.utils; //Uncomment this line for Overlay/XUL and bootstrap add-ons
const { TextDecoder, OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
function doAction(){
    var fileName = 'B:\\textFile.txt'
    //Read the file and log the contents to the console.
    readTextFile(fileName).then(console.log.bind(null,'readTextFile:'))
                          .catch(Cu.reportError);
    //Do it again, using the somewhat shorter syntax for a utf-8 encoded file
    readUtf8File(fileName).then(console.log.bind(null,'readUtf8File:'))
                          .catch(Cu.reportError);
}
function readTextFile(fileName){
    var decoder = new TextDecoder();
    return OS.File.read(fileName).then(array => decoder.decode(array));
}
function readUtf8File(fileName){
    //Using the somewhat shorter syntax for a utf-8 encoded file
    return OS.File.read(fileName, { encoding: "utf-8" }).then(text => text);
}

package.json:

{
    "title": "Read a text file",
    "name": "read-text-file",
    "version": "0.0.1",
    "description": "Reads a text file in two different ways.",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}

textFile.txt:

This is a text file line 1
Line 2
于 2016-09-06T00:06:35.063 回答