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
            Asked
            
        
        
            Active
            
        
            Viewed 3,907 times
        
    0
            
            
        - 
                    u want to programatic help or what – Balaswamy Vaddeman Jan 27 '12 at 11:44
 - 
                    yes, I am looking for a method just like saveURL is for saving the file. – Shevach Jan 27 '12 at 13:24
 
2 Answers
1
            
            
        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);
        Wladimir Palant
        
- 56,865
 - 12
 - 98
 - 126
 
1
            
            
        //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);
}
        Hemant Pawar
        
- 360
 - 1
 - 3
 - 14