如何在Firefox中更改下载文件夹目标?

时间:2012-01-27 11:40:45

标签: firefox firefox-addon

我正在开发一个firefox扩展程序,可以从facebook的当前页面下载图片。 我想知道是否有办法将默认下载目录更改为X.更优选的是要求用户输入一个。 感谢

2 个答案:

答案 0 :(得分:1)

此信息存储在首选项文件中,可以在about:config下查看。如果首选项browser.download.useDownloadDir设置为true,则会自动使用browser.download.dir中设置的下载目录。如果此变量设置为false,浏览器将询问用户保存文件的位置,并在对话框中最初选择browser.download.lastDir中设置目录。

但是,如果您正在编写扩展程序,则可能不希望使用这些首选项,而是让用户选择下载目录。您可以使用nsIFilePicker来表示这一点:

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

答案 1 :(得分: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);

}