The DownloadLastDir.jsm JavaScript code module lets you retrieve the path of the last directory into which a download occurred.

To use this, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/DownloadLastDir.jsm");

If you are using Addon SDK, you can import the code module as:

let { Cu } = require("chrome");
let DownloadLastDir = Cu.import("resource://gre/modules/DownloadLastDir.jsm").DownloadLastDir;

Once you've imported the module, you can then use the DownloadLastDir object it exports.

Using the DownloadLastDir object

To determine or set the path into which the last download occurred:

// file is an nsIFile
var file = DownloadLastDir.file;
DownloadLastDir.file = file;

You can also set and retrieve this information on a site-by-site basis.

To set the path, use setFile:

// file is an nsIFile
DownloadLastDir.setFile(uri, file);

To retrieve the path in Firefox 26 or later, use getFileAsync:

DownloadLastDir.getFileAsync(uri, function (file) {
  // file is an nsIFile
  console.log(file);
});

Deprecated since Gecko 26.0 To retrieve the path in Firefox 25 or earlier, use getFile:

// file is an nsIFile
var file = gDownloadLastDir.getFile(uri);
console.log(file);

Private browsing mode

When browsing normally, the browser uses the browser.download.lastDir preference to store the last download directory path. However, when private browsing mode is enabled, the last download directory path is instead maintained in memory, and the preference is not changed.

When the user exits private browsing mode, the last download directory value is reverted to the preference's value.

When history is cleared

When the user's browsing history is cleared, the value of the last download directory path is restored to the platform's default download directory path.

See also