• C# 利用 Geckofx60 实现下载


    利用 Geckofx-60 实现下载

    方案1. 根据Url直接下载

    private void GeckoDownload(string url, string fileFullName)
    {
        if (System.IO.File.Exists(fileFullName))
        { 
        System.IO.File.Delete(fileFullName);
        }
    
        Stream myStream = System.IO.File.Create(fileFullName);
        nsIURI source = IOService.CreateNsIUri(url);
        nsIURI dest = IOService.CreateNsIUri(new Uri(fileFullName).AbsoluteUri);
        nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(fileFullName));
        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, null, 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();
    }
    

    方案2. 弹出对话框提示下载

    public MainWindow()
    {
        InitializeComponent();
        Gecko.Xpcom.Initialize("Firefox");
        Gecko.GeckoPreferences.User["intl.accept_languages"] = "zh-CN,zh;q=0.9,en;q=0.8";
        _GeckoWebBrowser = new Gecko.GeckoWebBrowser();
        _GeckoWebBrowser.CreateControl();
        _GeckoWebBrowser.NoDefaultContextMenu = true; //禁用右键菜
        _GeckoWebBrowser.DocumentCompleted += _GeckoWebBrowser_DocumentCompleted;
        WindowsFormsHost.Child = _GeckoWebBrowser;
        
        // 弹出下载对话框
        Gecko.LauncherDialog.Download += new EventHandler<LauncherDialogEvent>(LauncherDialog_Download)
    }
    
    // 实现弹出对话框触发事件
    private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
    {
        nsILocalFileWin objTarget = Xpcom.CreateInstance<Gecko.nsILocalFileWin>("@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() == System.Windows.Forms.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();
            }
        }
    }
    
  • 相关阅读:
    所谓幸福
    kobuki 红外自动回充源码原理分析
    《SLAM机器人基础教程》第三章 单片机与STM32:电机码盘实现里程计实验
    《SLAM机器人基础教程》第三章 单片机与STM32:PWM电机转动实验
    《SLAM机器人基础教程》第三章 单片机与STM32:超声测距实验
    《SLAM机器人基础教程》第三章 单片机与STM32:ADC与电压检测实验
    《SLAM机器人基础教程》第三章 单片机与STM32:碰撞传感器实验
    《SLAM机器人基础教程》第三章 单片机与STM32:定时器实验
    《SLAM机器人基础教程》第三章 单片机与STM32:滴答延时实验使用SysTick实现时间戳
    《SLAM导航机器人基础》第三章:单片机与STM32:串口Printf打印实验
  • 原文地址:https://www.cnblogs.com/lqqgis/p/12644018.html
Copyright © 2020-2023  润新知