Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a win form with a WebBrowser control to log in to a secure web application. once in, I generate a report based on the selections made on the page, hit submit and it downloads as a excel file.

what I am wanting to do is capture the path to where the user saved the file so that I can use the file later in the application without having the user find and open the file again.

Is this possible?

What I have tried:

I have searched for hours but only find threads where people are looking to skip the dialog or specify the path.
Posted
Updated 12-Dec-19 17:29pm

1 solution

This may not be exactly what you are looking for, but give this a shot.

Use these Imports
VB
Imports System
Imports System.Net
Imports System.ComponentModel


Place inside of the form class where you have the webbrowser control, where 'WebBrowser1' is the name of your WebBrowser Control
VB
Private WithEvents downloadClient As WebClient

Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    If e.Url.Segments(e.Url.Segments.Length - 1).EndsWith("xlsx") Then
        e.Cancel = True
        Dim filepath As String

        Dim sd As New SaveFileDialog()
        sd.Filter = "Excel Workbook Files|*.xlsx"
        sd.FileName = e.Url.Segments(e.Url.Segments.Length - 1)
        If sd.ShowDialog() = DialogResult.OK Then
            filepath = sd.FileName
            downloadClient = New WebClient
            'downloadclient.DownloadFileCompleted = client_downloadComplete()

            downloadClient.DownloadFileAsync(e.Url, filepath)
            'store the file path here for later use
            Me.Text = filepath
        End If
        sd.Dispose()
    End If
End Sub

Private Sub downloadClient_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles downloadClient.DownloadFileCompleted
    MsgBox("File Downloaded - a")
End Sub


Private Sub downloadClient_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles downloadClient.DownloadProgressChanged
    ProgressBar1.Maximum = e.TotalBytesToReceive
    ProgressBar1.Value = e.BytesReceived
    Application.DoEvents()
End Sub



source that i found is in c#(marked as selected answer): https://stackoverflow.com/questions/15419632/download-a-file-through-the-webbrowser-control

Edit... Noticed a small error in which I forgot to initialize the WebClient Object

Edit 2... Have to use DownloadFileAsync so you can use UI updates in case you want to show a progress dialog of some sort. Also, added a progressbar example.
 
Share this answer
 
v3
Comments
Member 14685181 13-Dec-19 11:25am    
Thanks for the response. I tried something like that (without the progress bar, that is slick). This was the method that i wanted to use, but unfortunately I don't think WebClient will work for multiple reasons. But mostly because there is no URL to the file. The URL returned is to the page the makes the file, not a file.

How the web page works is the user makes several selections on the page and hits the "Run Report" button. The web page then uses the selections on the page to query the database and returns the results in an xlsx download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900