0
我可以在網站上抓取圖像的URL,但我想將圖像下載到本地驅動器。我真的想要標準的下載提示。使用Firefox Addon SDK(使用Javascript)的最佳方法是什麼?使用Firefox附加組件(SDK)保存網站圖像
我可以在網站上抓取圖像的URL,但我想將圖像下載到本地驅動器。我真的想要標準的下載提示。使用Firefox Addon SDK(使用Javascript)的最佳方法是什麼?使用Firefox附加組件(SDK)保存網站圖像
因此,使用從Firefox的nsIWebBrowserPersist docs中解除的一些代碼,我將它拼湊在一起。我不明白涉及的一些範圍問題,這不會提示用戶在哪裏或如何保存。爲了我的目的,它的工作原理,但我想如果有一個更好的解決方案。
function DownloadImage(aURLToDownload, aSaveToFile)
{
try {
// download from: aURLToDownload
var downloadURI = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(aURLToDownload, null, null);
console.log("Saving from: " + aURLToDownload);
// download destination
console.log("Saving as: " + aSaveToFile);
var outputFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
outputFile.initWithPath(aSaveToFile)
var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist);
persist.progressListener = {
// onComplete: function(){
// alert("Download complete: " + aSaveToFile);
// }
onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
var percentComplete = (aCurTotalProgress/aMaxTotalProgress)*100;
console.log(percentComplete +"%");
}
// onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
// }
};
persist.saveURI(downloadURI, null, null, null, "", outputFile);
} catch (e) {
console.log(e);
}
}