如何使用Visual Basic中的mozilla xul15处理gecko15上的下载?

时间:2013-11-11 12:40:49

标签: .net mozilla xulrunner gecko geckofx

我在我的visual basic应用程序中使用gecko15和mozilla xul15作为Web浏览器

有谁知道我可以处理下载吗?

因为,当我点击一个文件从该浏览器下载时没有任何反应。

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:0)

此外: @Form LOad事件: 加: AddHandler Gecko.LauncherDialog.Download,AddressOf LauncherDialog_Download

然后  Private Sub LauncherDialog_Download(ByVal sender As Object,ByVal e As Gecko.LauncherDialogEvent) e.cancel() 尝试 赶上前例外 最后  e.Navigate(Nothing)'防止异常的DM行为 结束尝试 结束子

希望有所帮助,

答案 1 :(得分:0)

这是我们所有人的头痛,不能让用户在嵌入式(vb net)gecko浏览器中下载文件。 'Kixpsider(我的朋友万岁)',一位匿名的天才,结束了这个麻烦。请按照以下代码以清晰的格式:

    Dim objTarget As Gecko.nsILocalFile = Gecko.Xpcom.CreateInstance(Of Gecko.nsILocalFile)("@mozilla.org/file/local;1")
    Dim tmp_Loc As String = Application.StartupPath & "\data"
    Dim sx As Object = Nothing
    Dim fx As String = ""
    Dim saveBox As New SaveFileDialog
    Dim win As Object = Gecko.Xpcom.GetService(Of Gecko.nsIWindowWatcher)("@mozilla.org/embedcomp/window-watcher;1")

    'win.OpenWindow(Nothing, "chrome://mozapps/content/downloads/downloads.xul", "Downloads", "chrome,resizable=yes,hide", Nothing)

    Using tmp As New Gecko.nsAString(tmp_Loc)
        objTarget.InitWithPath(tmp)
    End Using

    If e.Filename.Contains(".") Then
        sx = Strings.Split(e.Filename, ".")
        fx = sx(sx.Length - 1).ToUpper & " File (*." & sx(sx.Length - 1) & ")|*." & sx(sx.Length - 1)
    Else
        fx = "File (*.*)|*.*"
    End If

    savebox.Filter = fx '"HTML File (*.html)|*.html"
    savebox.Title = "Save File:"
    savebox.FileName = e.Filename
    If saveBox.ShowDialog <> System.Windows.Forms.DialogResult.OK And String.IsNullOrEmpty(saveBox.FileName) Then
        Exit Sub
    End If
    Dim source As Gecko.nsIURI = Gecko.IOService.CreateNsIUri(New Uri(e.Url).AbsoluteUri)
    Dim dest As Gecko.nsIURI = Gecko.IOService.CreateNsIUri(New Uri(saveBox.FileName).AbsoluteUri)
    Dim t As Gecko.nsAStringBase = DirectCast(New Gecko.nsAString(System.IO.Path.GetFileName(saveBox.FileName)), Gecko.nsAStringBase)

    Dim persist As Gecko.nsIWebBrowserPersist = Gecko.Xpcom.CreateInstance(Of Gecko.nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
    Dim DownloadMan As Gecko.nsIDownloadManager = Gecko.Xpcom.CreateInstance(Of Gecko.nsIDownloadManager)("@mozilla.org/download-manager;1")
    Dim downloadX As Gecko.nsIDownload = DownloadMan.AddDownload(0, source, dest, t, e.Mime, 0, Nothing, DirectCast(persist, Gecko.nsICancelable), False)

    If (downloadX IsNot Nothing) Then
        persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
        persist.SetProgressListenerAttribute(DirectCast(downloadX, Gecko.nsIWebProgressListener))
        persist.SaveURI(source, Nothing, Nothing, Nothing, Nothing, DirectCast(dest, Gecko.nsISupports), Nothing)
    End If

答案 2 :(得分:-1)

'注意:删除“'”

'Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)&amp; “\\ TMP”                         '如果不是System.IO.Directory.Exists(P)那么System.IO.Directory.CreateDirectory(P)

                    '                        Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
                    '                        Dim tmp_Loc As String = P & "\tmpdload"
                    '                        Using tmp As New nsAString(tmp_Loc)
                    ' objTarget.InitWithPath(tmp)
                    ' End Using

                    ' If e.Filename.Contains(".") Then
                    'S = Strings.Split(e.Filename, ".")
                    'F = S(S.Length - 1).ToUpper & " File (*." & S(S.Length - 1) & ")|*." & S(S.Length - 1)
                    'Else
                    '    F = "File (*.*)|*.*"
                    'End If

                    '    savebox.Filter = F '"HTML File (*.html)|*.html"
                    '    savebox.Title = "Save File:"
                    '    savebox.FileName = e.Filename
                    '    If savebox.ShowDialog = System.Windows.Forms.DialogResult.OK And Not String.IsNullOrEmpty(savebox.FileName) Then
                    ' Dim source As nsIURI = IOService.CreateNsIUri(New Uri(e.Url).AbsoluteUri)
                    ' Dim dest As nsIURI = IOService.CreateNsIUri(New Uri(savebox.FileName).AbsoluteUri)
                    ' Dim t As nsAStringBase = DirectCast(New nsAString(System.IO.Path.GetFileName(savebox.FileName)), nsAStringBase)
                    '
                    '                       Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
                    '                       Dim DownloadMan As nsIDownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
                    '                       Dim downloadX As nsIDownload = DownloadMan.AddDownload(0, source, dest, t, e.Mime, 0, Nothing, DirectCast(persist, nsICancelable), False) '

                    '                        If (downloadX IsNot Nothing) Then
                    'persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
                    'persist.SetProgressListenerAttribute(DirectCast(downloadX, nsIWebProgressListener))
                    'persist.SaveURI(source, Nothing, Nothing, Nothing, Nothing, DirectCast(dest, nsISupports), Nothing)

'显示FF下载管理器: 'Dim win = Xpcom.GetService(nsIWindowWatcher)(“@ mozilla.org/embedcomp/window-watcher;1”)                         'win.OpenWindow(Nothing,“chrome://mozapps/content/downloads/downloads.xul”,“Downloads”,“chrome,resizable = yes,hide”,Nothing)