在下载窗口中下载 Firefox 文件 - Firefox 扩展!

发布于 2024-11-14 19:23:01 字数 1590 浏览 3 评论 0原文

我有这个功能来下载文件:

function downloadFile(httpLoc)
{
try {
    // new obj_URI object
    var obj_URI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(httpLoc, null, null);

    // new file object
    var obj_TargetFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

    obj_TargetFile.initWithPath("d:\\te.zip");

    if(!obj_TargetFile.exists()) {
        obj_TargetFile.create(0x00,0644);
    }

    var obj_Persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);



    obj_Persist.progressListener = {
        onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress){
            var percentComplete = (aCurTotalProgress/aMaxTotalProgress)*100;
            var ele = document.getElementById("progress element");
            ele.innerHTML = percentComplete + "%";
        },
        onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
        }
    }

    // with persist flags
    const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
    const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
    obj_Persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;

    //save file to target
    obj_Persist.saveURI(obj_URI, null, null, null, "", obj_TargetFile);
}
catch (e) 
{
    alert(e);
}
}

文件正在下载,但我在 Firefox 下载窗口中看不到它。如何使文件出现在下载窗口中并查看进度?

I have this function to download a file :

function downloadFile(httpLoc)
{
try {
    // new obj_URI object
    var obj_URI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(httpLoc, null, null);

    // new file object
    var obj_TargetFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

    obj_TargetFile.initWithPath("d:\\te.zip");

    if(!obj_TargetFile.exists()) {
        obj_TargetFile.create(0x00,0644);
    }

    var obj_Persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);



    obj_Persist.progressListener = {
        onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress){
            var percentComplete = (aCurTotalProgress/aMaxTotalProgress)*100;
            var ele = document.getElementById("progress element");
            ele.innerHTML = percentComplete + "%";
        },
        onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
        }
    }

    // with persist flags
    const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
    const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
    obj_Persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;

    //save file to target
    obj_Persist.saveURI(obj_URI, null, null, null, "", obj_TargetFile);
}
catch (e) 
{
    alert(e);
}
}

The file is downloading but i don't see it in the Firefox Download Window. How do i make the file to appear in the Download Window and see the progress?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2024-11-21 19:23:01

这个应该给你一个良好的开端,以正确的方式做到这一点。

This should give you a good start on the right way to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文