none
want to suppress filedownload dialog box

    Question

  • hi,
    I am using webbrowser control to navigate and download file.
    what i am doing is when i click download button on my webbrowser control to download file,it give me popup to save or open file and after clicking save button it give me save as dialog box for where to save.

    Now what i want to do is when i click on download button in my webbrowser control ,i want file to be downloaded in some folder in my pc (suppose in D:/temp/) without above stated two folder.
    can anyone tell me is it possible to do so?
    the file i am trying to download is csv file.

    please guide me.
    Is it possible????????
    Friday, September 12, 2008 10:22 AM

Answers

  • I think you will have to get the web address and manually download the file yourself.  There are a couple of ways to do this.  The WebClient class is prob. one of the easiest methods to do this.

    Here's an example of downloading a file using the WebClient Class on a form that has a button named, "Button1".

     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim dest As String = "c:\temp\Googlepage.htm" 
            If Download("Http://www.google.com", dest) Then 
                If MessageBox.Show("Download successful. Open Folder?""Done", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 
                    System.Diagnostics.Process.Start(IO.Path.GetDirectoryName(dest))  
                End If 
            Else 
                MessageBox.Show("Download Failed due to the following exception: " & ex0.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)  
            End If 
        End Sub 
     
        Private ex0 As Exception  
     
        Function Download(ByVal Uri As StringByVal DestPath As StringAs Boolean 
            Dim result As Boolean 
            Using wc As New Net.WebClient  
                Try 
                    If Not IO.Directory.Exists(IO.Path.GetDirectoryName(DestPath)) Then 
                        IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(DestPath))  
                    End If 
                    wc.DownloadFile(Uri, DestPath)  
                    result = True 
                Catch ex As Exception  
                    ex0 = ex  
                    result = False 
                End Try 
            End Using  
            Return result  
        End Function 
     
     
    Monday, September 15, 2008 4:22 AM
  •  
    bhavin123000 said:
    I am using webbrowser control to navigate and download file.
    what i am doing is when i click download button on my webbrowser control to download file,it give me popup to save or open file and after clicking save button it give me save as dialog box for where to save.

    Now what i want to do is when i click on download button in my webbrowser control ,i want file to be downloaded in some folder in my pc (suppose in D:/temp/) without above stated two folder.
    can anyone tell me is it possible to do so?
    the file i am trying to download is csv file.


    Thank you Matt for your friendly help.

    Hi bhavin,

    Welcome to MSDN forums!

    It's a little difficult to suppress/disable such "File Download" dialog which is IE default behaviour.

    Here are two suggestions:

    1.  Use WebBrowser1.Document.GetElementsByTagName (or GetElementsById) to locate the csv file hyperlink element and retrieve its URL, then use My.Computer.Network.DownloadFile method to download it silently.

    2.  When WebBrowser pops up the 'File Download' dialog box, use SendKeys.SendWait("{Enter}") or SendKeys.SendWait("{Tab}") etc. to automate interactive process.

    e.g. Locate the "Download Handle (128 KB)" hyperlink and automatically download the Handle.zip file.

    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            WebBrowser1.Navigate("http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx")  
        End Sub 
     
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            ' Locate the "Download Handle (128 KB)" hyperlink and automatically download it  
            ' <a id="ctl00_mainContentContainer_ctl11" href="http://download.sysinternals.com/Files/Handle.zip"><strong>Download Handle (128 KB)</strong></a> < a> strong>< ><strong>Download Handle (128 KB)<>< a> strong>< ><strong>Download Handle (128 KB)<> 
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")  
            For Each curElement As HtmlElement In theElementCollection  
                Dim controlID As String = curElement.GetAttribute("InnerText").ToString  
                If controlID = "Download Handle (128 KB)" Then 
                    Dim linkURL As String = curElement.GetAttribute("href").ToString  
                    ' Download file in OverWrite mode  
                    My.Computer.Network.DownloadFile(linkURL, "D:\handle.zip"""""False, 10, True)  
                End If 
            Next 
        End Sub 
     
    End Class 


    To your scenario, you can locate certain hyperlinks whose URL contains ".csv" and then automatically download them like this:

    Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            WebBrowser1.Navigate("http://www.yourwebsite.com")  
        End Sub 
     
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            ' Locate certain hyperlinks whose URL contains ".csv" and automatically download them    
            ' <a href="xxx/xxx.csv">Link</a>   < a>   >Link<>< a>   >Link<> 
            Dim i As Int32 = 1  
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")  
            For Each curElement As HtmlElement In theElementCollection  
                Dim controlID As String = curElement.GetAttribute("InnerText").ToString  
                If controlID.Contains(".csv"Then 
                    Dim linkURL As String = curElement.GetAttribute("href").ToString  
                    ' Download file in OverWrite mode     
                    My.Computer.Network.DownloadFile(linkURL, "D:\myfile" & i & ".csv"""""False, 10, True)  
                    i = i + 1  
                End If 
            Next 
     
        End Sub 
     
    End Class 


    Please check these similar issues for reference:
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5454719e-b347-4ace-97ed-8d59ee48aab7/
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6cd60d72-0695-4408-bf40-d858aefc8945/


    Best regards,
    Martin Xie

    Thursday, September 18, 2008 10:20 AM

All replies

  • I think you will have to get the web address and manually download the file yourself.  There are a couple of ways to do this.  The WebClient class is prob. one of the easiest methods to do this.

    Here's an example of downloading a file using the WebClient Class on a form that has a button named, "Button1".

     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim dest As String = "c:\temp\Googlepage.htm" 
            If Download("Http://www.google.com", dest) Then 
                If MessageBox.Show("Download successful. Open Folder?""Done", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 
                    System.Diagnostics.Process.Start(IO.Path.GetDirectoryName(dest))  
                End If 
            Else 
                MessageBox.Show("Download Failed due to the following exception: " & ex0.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)  
            End If 
        End Sub 
     
        Private ex0 As Exception  
     
        Function Download(ByVal Uri As StringByVal DestPath As StringAs Boolean 
            Dim result As Boolean 
            Using wc As New Net.WebClient  
                Try 
                    If Not IO.Directory.Exists(IO.Path.GetDirectoryName(DestPath)) Then 
                        IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(DestPath))  
                    End If 
                    wc.DownloadFile(Uri, DestPath)  
                    result = True 
                Catch ex As Exception  
                    ex0 = ex  
                    result = False 
                End Try 
            End Using  
            Return result  
        End Function 
     
     
    Monday, September 15, 2008 4:22 AM
  • but sir,
    I dont have url of my   file.
     browser popup download file dialog box when i submit download button
    Monday, September 15, 2008 12:54 PM
  • I did not understand the problem.  I thought you had added a button in VB that the user clicked to download a file from a known link.

    I am not sure, but there might be a way for you to sub-class the form that the webbrowser is on, and detect when the filedialog popup is about to occur.  If you can do this, then you might be able to get the link that is being navigated to, cancel the popup, and open or save the file.

    The hardest thing would be do determine what Windows message value to intercept.  I have never sub-classed a form with a webbrowser on it.  Perhaps someone on "TheCodeProject.com" has made a custom webbrowser control that subclasses the webbrowser, but I have not seen one yet.

    Thursday, September 18, 2008 9:47 AM
  •  
    bhavin123000 said:
    I am using webbrowser control to navigate and download file.
    what i am doing is when i click download button on my webbrowser control to download file,it give me popup to save or open file and after clicking save button it give me save as dialog box for where to save.

    Now what i want to do is when i click on download button in my webbrowser control ,i want file to be downloaded in some folder in my pc (suppose in D:/temp/) without above stated two folder.
    can anyone tell me is it possible to do so?
    the file i am trying to download is csv file.


    Thank you Matt for your friendly help.

    Hi bhavin,

    Welcome to MSDN forums!

    It's a little difficult to suppress/disable such "File Download" dialog which is IE default behaviour.

    Here are two suggestions:

    1.  Use WebBrowser1.Document.GetElementsByTagName (or GetElementsById) to locate the csv file hyperlink element and retrieve its URL, then use My.Computer.Network.DownloadFile method to download it silently.

    2.  When WebBrowser pops up the 'File Download' dialog box, use SendKeys.SendWait("{Enter}") or SendKeys.SendWait("{Tab}") etc. to automate interactive process.

    e.g. Locate the "Download Handle (128 KB)" hyperlink and automatically download the Handle.zip file.

    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            WebBrowser1.Navigate("http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx")  
        End Sub 
     
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            ' Locate the "Download Handle (128 KB)" hyperlink and automatically download it  
            ' <a id="ctl00_mainContentContainer_ctl11" href="http://download.sysinternals.com/Files/Handle.zip"><strong>Download Handle (128 KB)</strong></a> < a> strong>< ><strong>Download Handle (128 KB)<>< a> strong>< ><strong>Download Handle (128 KB)<> 
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")  
            For Each curElement As HtmlElement In theElementCollection  
                Dim controlID As String = curElement.GetAttribute("InnerText").ToString  
                If controlID = "Download Handle (128 KB)" Then 
                    Dim linkURL As String = curElement.GetAttribute("href").ToString  
                    ' Download file in OverWrite mode  
                    My.Computer.Network.DownloadFile(linkURL, "D:\handle.zip"""""False, 10, True)  
                End If 
            Next 
        End Sub 
     
    End Class 


    To your scenario, you can locate certain hyperlinks whose URL contains ".csv" and then automatically download them like this:

    Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            WebBrowser1.Navigate("http://www.yourwebsite.com")  
        End Sub 
     
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            ' Locate certain hyperlinks whose URL contains ".csv" and automatically download them    
            ' <a href="xxx/xxx.csv">Link</a>   < a>   >Link<>< a>   >Link<> 
            Dim i As Int32 = 1  
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")  
            For Each curElement As HtmlElement In theElementCollection  
                Dim controlID As String = curElement.GetAttribute("InnerText").ToString  
                If controlID.Contains(".csv"Then 
                    Dim linkURL As String = curElement.GetAttribute("href").ToString  
                    ' Download file in OverWrite mode     
                    My.Computer.Network.DownloadFile(linkURL, "D:\myfile" & i & ".csv"""""False, 10, True)  
                    i = i + 1  
                End If 
            Next 
     
        End Sub 
     
    End Class 


    Please check these similar issues for reference:
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5454719e-b347-4ace-97ed-8d59ee48aab7/
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6cd60d72-0695-4408-bf40-d858aefc8945/


    Best regards,
    Martin Xie

    Thursday, September 18, 2008 10:20 AM
  • Haven't you guys heard, that there is a way, using "Subclassing and/or Hooking"...?

    I'm looking for examples using either subclassing or hooking, which allows you to intercept any dialog box anywhere in windows, including webbrowser control or IE's File Download dialog, look up the book "Subclassing and Hooking with Visual Basic" - it is available on google's ebook archive, searchable and readable, i am in the middle of reading it but I am trying to figure out the parameters and what to look for and how to implement this for the File Download dialog box in particular.

    And IDownloadManager implementation will not work for VB6, and i'm tired of seeing that as a solution - because it's not. Using subclassing, you can get all the details of the dialog box, and the details of the controls on the dialog box, then you can automatically change the values in the location section, and click the Save button, all WITHOUT THE DIALOG EVER BEING SHOWN TO ANY OF THE USERS, the download will begin, and the dialog will never be seen, because you are intercepting it and taking control of it instead of the default procedure that would take control of it.

    Search on google to find some examples, I've spent my last two days on this.

    You can read the whole book here (since its no longer copy-right protected):

    http://ebookbrowse.com/subclassing-and-hooking-with-visual-basic-pdf-d319133138

    Mainly for VB6 but includes chapters for VB.NET as well.

    There is a download button on the top if you prefer to download and view/read on your pdf reader or iPad/iPhone/Kindle :). Hopefully, someone figures out a solution, as i know there are solutions in .NET.

    Tuesday, October 16, 2012 7:00 PM