Shevach
Shevach

Reputation: 737

How to change the download folder destination in firefox?

I am developing a firefox extension that downloads pictures from the current page in facebook. I wonder if there is a way to change the default download directory to X. and more preferably to ask the user to input one. Thanks

Upvotes: 0

Views: 4734

Answers (2)

Hemant Pawar
Hemant Pawar

Reputation: 360

//give your file details in this line
downloadFile(title, url, fileType);

// Don't change anything below if you don't know what it does

function getDownloadFile(defaultString, fileType) 
{
    var nsIFilePicker = Components.interfaces.nsIFilePicker;

    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    fp.init(window, "Save As", nsIFilePicker.modeSave);
    try {
        var urlExt = defaultString.substr(defaultString.lastIndexOf(".")+1, 3);
        if (urlExt!=fileType) defaultString += "." + fileType
    }catch(ex){}

    fp.defaultString = defaultString;

    fp.appendFilter(fileType, "*." + fileType);
    var rv = fp.show();
    if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
      var file = fp.file;
      var path = fp.file.path;
      return file;
    }
    return null;
}
function downloadFile(title, url, fileType) 
{
    var file = getDownloadFile(title, fileType);

    var persist = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);  
    var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);  
    var uri = ios.newURI(url, null, null); 

    var target = ios.newFileURI(file) 
    var xfer = Components.classes["@mozilla.org/transfer;1"]  
                           .createInstance(Components.interfaces.nsITransfer);  
    xfer.init(uri, target, "", null, null, null, persist);  
    persist.progressListener = xfer; 
    persist.saveURI(uri, null, null, null, null, file);

}

Upvotes: 1

Wladimir Palant
Wladimir Palant

Reputation: 57671

This information is stored in the preferences file and can be viewed under about:config. If the preference browser.download.useDownloadDir is set to true the download directory set in browser.download.dir is used automatically. If this variable is set to false the browser will ask the user where to save the file, with the directory set in browser.download.lastDir initially selected in the dialog.

However, if you are writing an extension you probably don't want to use these preference but rather let the user choose a download directory. You would use nsIFilePicker for that, along these lines:

var filePicker = Components.classes["@mozilla.org/filepicker;1"]
                           .createInstance(Components.interfaces.nsIFilePicker);
filePicker.init(window, "Please choose a download directory", filePicker.modeGetFolder);
if (filePicker.show() == 0)
  window.alert("Directory chosen: " + filePicker.file.path);

Upvotes: 1

Related Questions