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.

4
  • Have you seen this? stackoverflow.com/questions/3749231/… I used JQuery in my firefox addon.
    – ysrb
    Jul 12, 2011 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. Jul 12, 2011 at 12:18
  • @Delan: saveURL definitely isn't an API function - it might be defined in an imported JavaScript module or something like that. Jul 12, 2011 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 ;) Jul 12, 2011 at 13:11

4 Answers 4

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.

1
  • 1
    As of FF 19, saveURI has received a 7th parameter, privacyContext. Usage here
    – Bora
    Jun 29, 2013 at 9:10
3

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

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");
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):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.