chrome.fileSystem.chooseEntry: Invalid calling page error

March 22, 2013

I've been experimenting with the fileSystem API for Chrome Apps and have been getting this error when calling the chrome.fileSystem.chooseEntry method:

Error during fileSystem.chooseEntry: Invalid calling page

The following code snippet causes the above error when the 'openfile' button located inside app.html is clicked:

chrome.app.runtime.onLaunched.addListener(function () {
  chrome.app.window.create('app.html', function(appWindow) {
    var htmlWindow = appWindow.contentWindow;

    htmlWindow.onload = function() {
        var document = this.document;

        document.getElementById('openfile').onclick = function() {

            // the next call will throw the error..
            chrome.fileSystem.chooseEntry(function (entry) { 
                // do something
            });

        };
    }
  });
});

Solution: This happens because the call for chrome.fileSystem.chooseEntry is using the chrome object of the background page. We should instead be using the chrome object of the HTML window.

chrome.app.runtime.onLaunched.addListener(function () {
  chrome.app.window.create('app.html', function(appWindow) {
    var htmlWindow = appWindow.contentWindow;

    htmlWindow.onload = function() {
        var document = this.document;

        document.getElementById('openfile').onclick = function() {

            // Notice how we're now using the 'chrome' object within 
            // the app.html window context

            htmlWindow.chrome.fileSystem.chooseEntry(function (entry) { 
                // do something
            });
        };
    }
  });
});