7

I'm new to Firefox addon development, and it's going well so far, but I'm stuck on how to, essentially, download a file from the Web, given a URI, and save it to disk. Mozilla's MDN documentation has information on how to upload files, but the downloading files section is empty and yet to be written. Sadly, I haven't found any documentation which describes how to do this.

Does anyone know of relevant documentation on how to do this?


The old Facebook Photo Album Downloader addon uses this function call in its overlay JavaScript:

saveURL(images[i].replace(/\/s/g, "/n"), null, null, false, true, null);

Obviously, the first argument is the URI to request. The saveURL function isn't defined anywhere, so I assume it's an extension API function. I have tried it in my new addon, and it does work. I would, however, like to know what the other arguments mean.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • Have you seen this? http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery I used JQuery in my firefox addon. – ysrb Jul 12 '11 at 12:16
  • Sadly this won't really work for me, as I have no access to the web server in question, and thus can't modify the MIME type that the files are sent with. There must be a native API method to do this for extensions -- I just can't find it. – Delan Azabani Jul 12 '11 at 12:18
  • @Delan: `saveURL` definitely isn't an API function - it might be defined in an imported JavaScript module or something like that. – Wladimir Palant Jul 12 '11 at 13:03
  • 1
    I see. That's interesting because, I've tried it in my new addon and it works. Of course, it probably isn't the most recommended way, as I can see from the answers ;) – Delan Azabani Jul 12 '11 at 13:11

4 Answers4

5

The standard way to do this is with nsIWebBrowserPersist:

var persist =
  Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
  createInstance(Ci.nsIWebBrowserPersist);
persist.saveURI(serverURI, null, null, null, "", targetFile);

See https://developer.mozilla.org/en/Code_snippets/Downloading_Files for more info.

Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54
  • 1
    As of FF 19, saveURI has received a 7th parameter, privacyContext. Usage [here](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWebBrowserPersist#saveURI%28%29) – Bora Jun 29 '13 at 09:10
3

There actually is some MDN documentation on this: https://developer.mozilla.org/en/Code_snippets/Downloading_Files.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
1

Here's an easy copy/paste option for anyone looking for a quick solution without any further messing about. Put it in your main.js and change the filename, directory and url.

function DownloadFile(sLocalFileName, sRemoteFileName)
{
    var saveToDirectory = 'C:\\Users\\louis\\downloads\\';

    var chrome = require("chrome");

    var oIOService = chrome.Cc["@mozilla.org/network/io-service;1"].getService(chrome.Ci.nsIIOService)

    var oLocalFile = chrome.Cc["@mozilla.org/file/local;1"].createInstance(chrome.Ci.nsILocalFile);
    oLocalFile.initWithPath(saveToDirectory + sLocalFileName);

    var oDownloadObserver = {onDownloadComplete: function(nsIDownloader, nsresult, oFile) {console.log('download complete...')}};

    var oDownloader = chrome.Cc["@mozilla.org/network/downloader;1"].createInstance();
    oDownloader.QueryInterface(chrome.Ci.nsIDownloader);
    oDownloader.init(oDownloadObserver, oLocalFile);

    var oHttpChannel = oIOService.newChannel(sRemoteFileName, "", null);
    oHttpChannel.QueryInterface(chrome.Ci.nsIHttpChannel);
    oHttpChannel.asyncOpen(oDownloader, oLocalFile);    

}
DownloadFile("saveAsThis.mp3","http://domain.com/file.mp3");
louisinhongkong
  • 551
  • 1
  • 6
  • 13
0

As of 2015, the APIs for managing (starting, stopping, etc.) downloads have changed since this question was answered. The new APIs are (links to documentation on MDN):

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121