I am using gecko15 with mozilla xul15 as a web browser on my visual basic application
anyone knows how i can handle downloads please ?
because, when i click on a file to download from that browser nothing happens.
thanks a lot for any help.
Also: @Form LOad Event: Add: AddHandler Gecko.LauncherDialog.Download, AddressOf LauncherDialog_Download
Then Private Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent) e.cancel() try catch ex as exception finally e.Navigate(Nothing) 'prevent unusual DM behavior end try End Sub
Hope that helps,
This was the headache of all us, not to be able to let user to download a file in embedded (vb net) gecko browser. 'Kixpsider (long live my friend)', an anonymous genius, ended this trouble. Please follow code below in clear format:
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
'Note: Remove "'"
' Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\\tmp" ' If Not System.IO.Directory.Exists(P) Then 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)
'To show the FF Download Manager: 'Dim win = Xpcom.GetService(Of nsIWindowWatcher)("@mozilla.org/embedcomp/window-watcher;1") ' win.OpenWindow(Nothing, "chrome://mozapps/content/downloads/downloads.xul", "Downloads", "chrome,resizable=yes,hide", Nothing)