如何使用firefox插件中的javascript将当前网页保存到所需位置。我有一个按钮。当用户点击按钮时,我想要一个资源管理器窗口打开并将文件保存到所需的位置。
答案 0 :(得分: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;
}