实际上,下载文件有点复杂。我无法为您发布我自己的VB代码(我做C#)示例/解决方案,但下面的链接中有工作示例:
简而言之,您需要做的是提供下载管理器的实现,并自己处理文件持久性、进度等。但是,它主要是样板代码,您应该能够根据示例找出要执行的操作:
代码来自此bitbucket问题,我将发布此代码,以防链接失效:
https://bitbucket.org/geckofx/geckofx-45.0/issues/15/downloading-files-with-geckofx-45
Imports Gecko
Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
' SU WINDOWS XP INSTALLARE Visual C++ Redistributable per Visual Studio 2013 vcredist_x86.exe
Public Class Form1
'ask for save
'user_pref("browser.download.useDownloadDir", false);
'
'set download folder
'user_pref("browser.download.dir", "C:\\Download");
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Browser.NoDefaultContextMenu = True
Browser.Navigate("www.google.it")
AddHandler Gecko.LauncherDialog.Download, AddressOf LauncherDialog_Download
TextBox1.Text = "https://download.microsoft.com/download/7/2/9/7290EA05-DC56-4BED-9400-138C5701F174/WS2016LicensingDatasheet.pdf"
'TEST EDIT USER AGENT
Dim sUserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.1; pl; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)"
Gecko.GeckoPreferences.User("general.useragent.override") = sUserAgent
'THIS DISABLE THE PDF PLUGIN THAT OPEN THE PDF ON BROWSER
Gecko.GeckoPreferences.User("plugin.disable_full_page_plugin_for_types") = "application/pdf"
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Browser.Navigate(TextBox1.Text)
End If
End Sub
Dim DownloadMan As nsIDownloadManager = Nothing
Private Sub LauncherDialog_Download(sender As Object, e As Gecko.LauncherDialogEvent)
Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
Using tmp As New nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + vbTab & "emp.tmp")
objTarget.InitWithPath(tmp)
End Using
Dim source As nsIURI = IOService.CreateNsIUri(e.Url)
Dim dest As nsIURI = IOService.CreateNsIUri(New Uri(Application.StartupPath & "\download\" & e.Filename).AbsoluteUri)
Dim t As nsAStringBase = DirectCast(New nsAString(System.IO.Path.GetFileName(e.Filename)), nsAStringBase)
Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
Dim nst As nsITransfer = Xpcom.CreateInstance(Of nsITransfer)("@mozilla.org/transfer;1")
Dim m As New MyGekoClass
m.Init(source, dest, t, e.Mime, 0, Nothing, persist, False)
nst = m
If nst IsNot Nothing Then
persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
persist.SetProgressListenerAttribute(DirectCast(nst, nsIWebProgressListener))
persist.SaveURI(source, Nothing, Nothing, CUInt(Gecko.nsIHttpChannelConsts.REFERRER_POLICY_NO_REFERRER), Nothing, Nothing, _
DirectCast(dest, nsISupports), Nothing)
End If
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return target
End Function
Private Sub Browser_ObserveHttpModifyRequest(sender As Object, e As GeckoObserveHttpModifyRequestEventArgs) Handles Browser.ObserveHttpModifyRequest
'here you can stop the download of same extension by using e.Cancel=True
Dim str = e.Uri.ToString
If str.Substring(str.Length - 4) = ".pdf" Then
'e.Cancel = True
End If
End Sub
Private Sub cmdGo_Click(sender As Object, e As EventArgs) Handles cmdGo.Click
Browser.Navigate(TextBox1.Text)
End Sub
End Class
Class MyGekoClass
Implements nsITransfer
Public Sub Init(aSource As nsIURI, aTarget As nsIURI, aDisplayName As nsAStringBase, aMIMEInfo As nsIMIMEInfo, startTime As Long, aTempFile As nsIFile, aCancelable As nsICancelable, aIsPrivate As Boolean) Implements nsITransfer.Init
End Sub
Public Sub OnLocationChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsITransfer.OnLocationChange
End Sub
Public Sub OnProgressChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsITransfer.OnProgressChange
End Sub
Public Sub OnProgressChange64(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Long, aMaxSelfProgress As Long, aCurTotalProgress As Long, aMaxTotalProgress As Long) Implements nsITransfer.OnProgressChange64
End Sub
Public Function OnRefreshAttempted(aWebProgress As nsIWebProgress, aRefreshURI As nsIURI, aMillis As Integer, aSameURI As Boolean) As Boolean Implements nsITransfer.OnRefreshAttempted
End Function
Public Sub OnSecurityChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsITransfer.OnSecurityChange
End Sub
Public Sub OnStateChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsITransfer.OnStateChange
End Sub
Public Sub OnStatusChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsITransfer.OnStatusChange
End Sub
Public Sub SetRedirects(aRedirects As nsIArray) Implements nsITransfer.SetRedirects
End Sub
Public Sub SetSha256Hash(aHash As nsACStringBase) Implements nsITransfer.SetSha256Hash
End Sub
Public Sub SetSignatureInfo(aSignatureInfo As nsIArray) Implements nsITransfer.SetSignatureInfo
End Sub
Public Sub OnLocationChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsIWebProgressListener.OnLocationChange
End Sub
Public Sub OnProgressChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsIWebProgressListener.OnProgressChange
End Sub
Public Sub OnSecurityChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsIWebProgressListener.OnSecurityChange
End Sub
Public Sub OnStateChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsIWebProgressListener.OnStateChange
End Sub
Public Sub OnStatusChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsIWebProgressListener.OnStatusChange
End Sub
Public Sub OnLocationChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsIWebProgressListener2.OnLocationChange
End Sub
Public Sub OnProgressChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsIWebProgressListener2.OnProgressChange
MsgBox("PROGRESS: " & aCurSelfProgress)
End Sub
Public Sub OnProgressChange641(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Long, aMaxSelfProgress As Long, aCurTotalProgress As Long, aMaxTotalProgress As Long) Implements nsIWebProgressListener2.OnProgressChange64
MsgBox("PROGRESS: " & aCurSelfProgress & "/" & aMaxTotalProgress)
End Sub
Public Function OnRefreshAttempted1(aWebProgress As nsIWebProgress, aRefreshURI As nsIURI, aMillis As Integer, aSameURI As Boolean) As Boolean Implements nsIWebProgressListener2.OnRefreshAttempted
End Function
Public Sub OnSecurityChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsIWebProgressListener2.OnSecurityChange
End Sub
Public Sub OnStateChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsIWebProgressListener2.OnStateChange
End Sub
Public Sub OnStatusChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsIWebProgressListener2.OnStatusChange
End Sub
End Class