开发者

firefox download file in the Download window - Firefox Extension!

开发者 https://www.devze.com 2023-03-11 21:34 出处:网络
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.i

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消