问题
I'm working on a restartless firefox addon. Using firefox developer edition v36. The MDN page on the Dom File API claims that you can import File
as:
const {File, Services} = Cu.import('resource://gre/modules/Services.jsm', {});
But Services.jsm clearly does not export a File Object. I also tried:
new contentWindow.File( filename )
But this gives a very descriptive NS_ERROR_FAILURE.
Any clues are welcome, thanks
回答1:
Try this:
Cu.importGlobalProperties(["File"]);
MDN :: Components.utils.importGlobalProperties
回答2:
Noitidart found a workaround:
const { Services } = Cu.import('resource://gre/modules/Services.jsm', {})
// And you're holding on to the constructor straight away
//
var domfile = Services.appShell.hiddenDOMWindow.File( filename )
I have found another workaround at the same time:
// Where window is a contentWindow.
//
var domWindowUtils = window.QueryInterface( Ci.nsIInterfaceRequestor)
.getInterface( Ci.nsIDOMWindowUtils)
var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils
var nsifile = new FileUtils.File( fileName )
var domfile = domWindowUtils.wrapDOMFile( nsifile )
Both approaches are kind of workarounds because they mean you need a loaded window. Currently we found no way to get an interface to it without a window.
来源:https://stackoverflow.com/questions/27893399/how-do-i-use-the-dom-file-api-from-privileged-code