从代码中按文件下载对话框中的保存按钮

时间:2014-08-07 14:46:42

标签: vb.net

我有一个WebBrowser控件,它会自动将我登录到一个网站并尝试自动下载ZIP文件。一切都运行正常,除了当它试图下载ZIP时,它会弹出一个对话框,询问我是要保存文件还是打开它(就像在IE中一样)。我想要做的是自动将ZIP文件下载到我选择的文件中。我似乎无法弄清楚如何压制对话框并自动捕获下载。当我尝试使用WebClient进行下载时,它会保存html页面而不是zip。

我需要在文件下载弹出窗口中按save botton。在这里,我无法使用sendKey。

请帮帮我... 感谢

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Try      
        If (Label6.Text <> "Got Url") Then  
            If WebBrowser1.Document.Title = "Login" Then
                If ls_IsloginDone Then Exit Sub                    
                WebBrowser1.Document.GetElementById("user").SetAttribute("value", "user")
                WebBrowser1.Document.GetElementById("pwd").SetAttribute("value", "pwd")
                WebBrowser1.Document.GetElementById("btnLogin").InvokeMember("click")                    
                ls_IsloginDone = True
            End If
            If WebBrowser1.Document.Title = "Home" Then
                WebBrowser1.Document.GetElementById("menuBar_productLink").InvokeMember("click")
            ElseIf WebBrowser1.Document.Title.Contains("Downloads") Then                    
                Dim ls_ulr1 As String = String.Empty
                If Not IsNothing(WebBrowser1.Document.GetElementById("repDownloads__ctl1_hypVer")) Then
                    Dim lsohtml As String = WebBrowser1.Document.GetElementById("repDownloads__ctl1_hypVer").OuterHtml
                    If lsohtml.Contains("downloadURL") Then
                        Dim lsindex As String = lsohtml.IndexOf(""">")
                        Try
                            ls_ulr1 = lsohtml.Substring(lsohtml.IndexOf("downloadURL=") + 13, Math.Abs(lsindex - lsohtml.IndexOf("downloadURL=")) - 13)
                        Catch ex As Exception

                        End Try
                    End If                    
                End If
                If Not String.IsNullOrEmpty(ls_ulr1) Then                        
                    Label6.Text = "Got Url"                        
                    WebBrowser1.Navigate("http://some.website.com/" & ls_ulr1)
                End If                   
            End If
        ElseIf (Label6.Text = "Got Url") Then
            If Not IsNothing(WebBrowser1.Document.GetElementById("hypDownload")) Then
                Label6.Text = "Url fired"
                Dim href As String = WebBrowser1.Document.GetElementById("hypDownload").GetAttribute("href")                    
                WebBrowser1.Document.GetElementById("hypDownload").InvokeMember("click")                    
            End If
        ElseIf isDwldComplete = True Then
            Label6.Text = "Download completed"
            MessageBox.Show("Download complete")
        End If            
    Catch ex As Exception
        ls_err += " " & ex.ToString & vbNewLine
    End Try
    If Not String.IsNullOrEmpty(ls_err) Then
        TextBox2.Text = ls_err
        TextBox2.ForeColor = Color.Red
    End If
End Sub

0 个答案:

没有答案