如何在 Firefox 插件中使用 javascript 将当前网页保存到所需位置。我有一个按钮。当用户单击按钮时,我希望打开资源管理器窗口并将文件保存到所需位置。
问问题
660 次
1 回答
1
将此函数downloadFile(title, url, fileType)
与适当的参数一起使用
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);
}
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;
}
于 2012-05-10T10:41:13.253 回答