2016-11-04 78 views
1

我使用Gecko嵌入式瀏覽器,並在最終步驟中單擊鏈接並下載zip文件。除了瀏覽器使用默認程序打開文件外,它運行良好。如何防止瀏覽器打開下載的zip文件?Firefox/Gecko嵌入式瀏覽器

回答

0

您需要訂閱LauncherDialog事件,並提供自己的實現:

原來的答覆這裏:

private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e) 
{ 
    nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1"); 

    using (nsAString tmp = new nsAString(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\temp.tmp")) 
    { 
     objTarget.InitWithPath(tmp); 
    } 

    //Save file dialog 
    Stream myStream; 
    SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

    saveFileDialog1.Filter = "All files (*.*)|*.*"; 
    saveFileDialog1.FilterIndex = 2; 
    saveFileDialog1.RestoreDirectory = true; 
    saveFileDialog1.FileName = e.Filename; 

    if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     if ((myStream = saveFileDialog1.OpenFile()) != null) 
     { 
      nsIURI source = IOService.CreateNsIUri(e.Url); 
      nsIURI dest = IOService.CreateNsIUri(new Uri(@saveFileDialog1.FileName).AbsoluteUri); 
      nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(@saveFileDialog1.FileName)); 

      nsIWebBrowserPersist persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1"); 

      nsITransfer nst = Xpcom.CreateInstance<nsITransfer>("@mozilla.org/transfer;1"); 
      nst.Init(source, dest, t, e.Mime, 0, null, persist, false); 

      if (nst != null) 
      { 
       persist.SetPersistFlagsAttribute(2 | 32 | 16384); 
       persist.SetProgressListenerAttribute((nsIWebProgressListener)nst); 
       persist.SaveURI(source, null, null, (uint)Gecko.nsIHttpChannelConsts.REFERRER_POLICY_NO_REFERRER, null, null, (nsISupports)dest, null); 
      } 

      myStream.Close(); 
     } 
    } 
} 
https://bitbucket.org/geckofx/geckofx-45.0/issues/15/downloading-files-with-geckofx-45

以供將來參考郵政代碼