admin';?>

首页 / Development & communication

File Download

By admin •  2018-09-26 22:45:19 •  186次点击
以下代码来自网络,尚未测试。
I dont test code below, which come from internet.
/mnt/e/firefox-52.9/src/embedding/components/webbrowserpersist/nsWebBrowserPersist.cpp

//https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebBrowserPersist
function downloadFile(httpLoc, sourceWindow) {
  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);

    //set file with path
    obj_TargetFile.initWithPath("c:\temp\test.pdf");
    //if file doesn't exist, create
    if(!obj_TargetFile.exists()) {
      obj_TargetFile.create(0x00,0644);
    }

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

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

    var privacyContext = sourceWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                     .getInterface(Components.interfaces.nsIWebNavigation)
                                     .QueryInterface(Components.interfaces.nsILoadContext);

    //save file to target
    obj_Persist.saveURI(obj_URI,null,null,null,null,obj_TargetFile,privacyContext);
    // Starting with Firefox 36 saveURI takes 8 parameters when called. aReferrerPolicy was added.
    // obj_Persist.saveURI(obj_URI,null,null,null,null,null,obj_TargetFile,privacyContext);
  } catch (e) {
    alert(e);
  }
}

nsIDownloadManagerUI
https://developer.mozilla.org/samples/SimplePersist.cpp
nsresult DownloadFile(const char *aURI, const char *aDest)
{
    NS_ENSURE_ARG_POINTER(aURI);
    NS_ENSURE_ARG_POINTER(aDest);
 
    // Simple persist only supports one op at a time.
    NS_ENSURE_FALSE(gPersist, NS_ERROR_UNEXPECTED);
 
    // Create persist object
    nsresult rv;
    nsCOMPtr<nsIWebBrowserPersist> persist = do_CreateInstance(NS_WEBBROWSERPERSIST_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
 
    // Set listener
    nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(new nsWebProgressListener);
    persist->SetProgressListener(listener);
 
    // Flags to control persist operation
    PRUint32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_BYPASS_CACHE; // Force network for debug purposes
    persist->SetPersistFlags(flags);
 
    // Create uri object
    nsCOMPtr<nsIURI> uri;
    rv = NS_NewURI(getter_AddRefs(uri), aURI);
    NS_ENSURE_SUCCESS(rv, rv);
 
    // Create file object
    nsCOMPtr<nsILocalFile> file;
    rv = NS_NewLocalFile(NS_ConvertUTF8toUCS2(aDest), PR_FALSE, getter_AddRefs(file));
    NS_ENSURE_SUCCESS(rv, rv);
 
    persist->SaveURI(uri, nsnull, nsnull, nsnull, nsnull, file);
 
    // Hold a ref so the operation can complete
    gPersist = persist;
 
    // EXERCISE: Add a native message processing loop here to make this
    //           operation synchronous.
 
    return NS_OK;
}

0 回复 | 直到2019-06-26 16:07添加回复

回复

登录发表 or 还没有账号?去注册

他山跨平台混合应用开发框架

现在注册 已注册请 登入
Development & communication

节点列表