How can I specify the download manager to save to the last directory used?

Viewed 554

I want to invoke a download such that it will open the file chooser dialog in the last place they've saved a file to. This is the functionality you get when you download something manually as a user and have the browser setting enabled to "Ask where to save each file before downloading".

E.g. if you save a file to C:/Pictures, then use my addon to download something, it should bring up the file chooser dialog and start at C:/Pictures.

A related bug report for Chrome is here: https://bugs.chromium.org/p/chromium/issues/detail?id=838952&q=chrome.downloads.download&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified

For a small workaround to do this on Chrome, I specify saveAs: true in my download options:

chrome.downloads.download({
  url: '...',
  saveAs: true,
});

This gives me the functionality I'm looking for on Chrome. However, on Firefox, it will always start the file chooser dialog in C:/Users/MikeY/Downloads, since that is my default downloads directory. If I don't specify saveAs: true for Chrome, it will do the same thing.

Is there any similar workaround for Firefox? Perhaps I can read the browser setting browser.download.lastDir somehow and specify that in filename?

2 Answers

There is no such option due to security. As you have mentioned, the possibilities are:

  • Use Save As
  • If specified, download to the folder specified in Options -> Files and Applications -> Downloads -> Save File to

However, if download folder is specified, you can set your preferred sub-directory of the download folder

Even if you have access to browser.download.lastDir, you can not specify it in downloads.download() API.

This might be what you're looking for, i.e. download.lastDir.savePerSite. This determines if it will download to the last directory saved to regardless of context, or the website you are on when downloading. If you wish to always have the save directory be the last directory saved to, set this to false.

EDIT: As a followup, you cannot specify or obtain this directly for use in the code of the extension for any additional functionality. Using this will have whatever is to be downloaded by firefox client be the download.lastdir. If your only use for it is in the 'url' assignment for downloading, then this provides all functionality. But like the user above said, you cannot actually retrieve this due to security reasons.

Related