使用VB.NET中的CefSharp下载多个文件

时间:2017-03-31 19:49:22

标签: vb.net winforms cefsharp

如何使用CefSharp下载多个文件。

我需要从页面下载多个文件,我执行javaScript来执行此操作。 首先,我在Chrome中证明,一开始不起作用,只下载第一个链接。我修复了将Chrome <{3}} 的属性自动下载更改为允许所有网站自动下载多个文件

首先,使用CefSharp,我无法下载任何内容,使用此代码我修复了它。

Public Class DownloadHandler
Implements IDownloadHandler

Public Event OnBeforeDownloadFired As EventHandler(Of DownloadItem)
Public Event OnDownloadUpdatedFired As EventHandler(Of DownloadItem)

Public Sub OnBeforeDownload(browser As IBrowser, downloadItem As DownloadItem, callback As IBeforeDownloadCallback) Implements IDownloadHandler.OnBeforeDownload
    RaiseEvent OnBeforeDownloadFired(Me, downloadItem)

    If Not callback.IsDisposed Then
        Using callback
            callback.[Continue](downloadItem.SuggestedFileName, showDialog:=False)
        End Using
    End If
End Sub

Public Sub OnDownloadUpdated(browser As IBrowser, downloadItem As DownloadItem, callback As IDownloadItemCallback) Implements IDownloadHandler.OnDownloadUpdated
    RaiseEvent OnDownloadUpdatedFired(Me, downloadItem)
End Sub
End Class

但我的问题是它只下载第一个链接,我需要下载多个文件。如何让CefSharp下载多个文件?

1 个答案:

答案 0 :(得分:-1)

您是什么意思&#39;从网页下载多个文件?&#39;您只是从公共网站下载文件吗?我可以想到几种方法来做到这一点。如果您想循环浏览一堆链接并下载所有文件,可以在Excel中设置清单列表,如下图所示。

enter image description here

然后,运行以下宏。

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
  "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
    szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub DownloadFilefromWeb()
    Dim strSavePath As String
    Dim URL As String, ext As String
    Dim buf, ret As Long
    URL = Worksheets("Sheet1").Range("A2").Value
    buf = Split(URL, ".")
    ext = buf(UBound(buf))
    strSavePath = "C:\Users\rshuell\Desktop\Downloads\" & "DownloadedFile." & ext
    ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
    If ret = 0 Then
        MsgBox "Download has been succeed!"
    Else
        MsgBox "Error"
    End If
End Sub

现在,如果您只想下载一个文件,请运行以下脚本。

Sub DownloadFileWithVBA()

Dim myURL As String
'Right-click on the link named 'Sample Address File'
'Click 'Copy Link Location'
'Paste the link below
myURL = "http://databases.about.com/library/samples/address.xls"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile ("C:\Users\Excel\Desktop\address.xls")
    oStream.Close

End Sub

这是Excel&amp; VBA,而不是VB.NET,但如果它能帮助您实现目标,请将其标记为答案!