Results 1 to 5 of 5

Thread: [RESOLVED] Webview2 Download Not Firing

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Resolved [RESOLVED] Webview2 Download Not Firing

    ADMIN: Please move to VB.NET Sub Forum. Apologies. Many thanks!

    Using WebView2 (webVwLAF), I am trying to manually download and open a PDF from a postback link from a site. Unfortunately, when I use "wait(10) to allow the download to finish and to be able to open the file thereafter, the "click" in ExecuteScriptAsync doesn't fire and download doesn't start. But, if I remove "wait(10)", click ExecuteScriptAsync fires up the download.

    Any help on this? Or any recommendations/sample how to catch download finished event of webview2?

    Thanks!



    Code:
    Private Sub webVwLAF_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVwLAF.NavigationCompleted
    
            webVwLAF.Select()
            If FirstRunLAF Then
                webVwLAF.ExecuteScriptAsync("document.getElementById('lnklaf').click();")
    
                Try
                    Wait(10)
                    WebAddress = "C:/Users/pga/Downloads/Laf.pdf"
                    webVwLAF.Source = New Uri(WebAddress)
                    webVwLAF.CoreWebView2.Navigate(WebAddress)
                Catch ex As UriFormatException
                    MessageBox.Show("Full URL ex -> HTTP[S]")
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error...")
                End Try
    
                FirstRunLAF = False
            End If
    
    
        End Sub
    Public Sub Wait(ByVal seconds As Double)
            Static start As Date
            start = Now()
            Do While Now() < start.AddSeconds(seconds)
                System.Windows.Forms.Application.DoEvents()
            Loop
        End Sub
    Last edited by pidyok; Mar 26th, 2021 at 12:36 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Webview2 Download Not Firing

    Quote Originally Posted by pidyok View Post
    ADMIN: Please move to VB.NET Sub Forum. Apologies. Many thanks!
    Done

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webview2 Download Not Firing

    Do not EVER use a busy-wait like that. A busy-wait is when you are constantly doing work to achieve nothing. That loop will use 100% of the CPU in order to do nothing. That's bad for all sorts of reasons.

    You could use a Timer with an Interval of 10 seconds and then do the remaining work on the Tick event. These days, you could also use Task.Delay and not even have to split the code. You could use it as a drop-in replacement for your horrible Wait method, although you will have to use Async/Await.
    vb.net Code:
    1. Private Async Sub webVwLAF_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVwLAF.NavigationCompleted
    2.  
    3.     '...
    4.  
    5.     Await Task.Delay(TimeSpan.FromSeconds(10))
    6.  
    7.     '...
    8.  
    9. End If

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Re: Webview2 Download Not Firing

    Thanks for the heads up @jmcilhinney. You've been of great help to me. I'll take note of that.

    Meanwhile, I figured it out thru ideas from another forum conversation. The finished code below is for those wanting to settle for a webview2 workaround of opening a downloaded file from a postback link. Not sure if it is optimized, maybe a laughable one.

    Admin, pls mark this thread solved. Thanks!

    OT Rant - Sad that MS won't update VB.NET anymore. VB.NET is an easy to read mainstay language for people like me who rarely/very occasionally program and rely on samples/help over the net to get things done at proactively at work (programming isn't really my job).


    Code:
    
        Delegate Sub NavigationDelegate()
        Private NavigationMethod As New NavigationDelegate(AddressOf Navigation)
        Public UpdateThread As Thread
        Public UpdateThreadStart As New ThreadStart(AddressOf LAFWaitPDF)
    
        Sub LoadWeather()
            Try
               
                'LAF
                webVwLAF.Select()
                WebAddress = "HTTPS://WWW.AVMET.AE"
                webVwLAF.Source = New Uri(WebAddress)
    
                UpdateThread = New Thread(UpdateThreadStart)
                UpdateThread.IsBackground = True
                UpdateThread.Name = "UpdateThread"
                UpdateThread.Start()
    
                'webVwLAF.CoreWebView2.Navigate(WebAddress)
            Catch ex As UriFormatException
                MessageBox.Show("Full URL ex -> HTTP[S]")
            Catch ex As Exception
                MessageBox.Show(err.number & " - " & ex.Message, "Error...")
            End Try
    
        End Sub
    
        Async Sub LAFWaitPDF()
            Try
                Await Task.Delay(TimeSpan.FromSeconds(10))
                WebAddress = "C:/Users/pgara/Downloads/Laf.pdf" 'LAF Download Location
                webVwLAF.Invoke(NavigationMethod)
            Catch ex As UriFormatException
                MessageBox.Show("Full URL ex -> HTTP[S]")
            Catch ex As Exception
                MessageBox.Show(Err.Number & " - " & ex.Message, "Error...")
            End Try
        End Sub
    
        Private Sub Navigation()
            webVwLAF.Source = New Uri(WebAddress)
        End Sub
        
        Private Sub webVwLAF_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVwLAF.NavigationCompleted
            webVwLAF.Select()
            If FirstRunLAF Then
                webVwLAF.ExecuteScriptAsync("document.getElementById('lnklaf').click();") 'UnComment on deployment
                FirstRunLAF = False
            End If
        End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webview2 Download Not Firing

    Quote Originally Posted by pidyok View Post
    Admin, pls mark this thread solved.
    Use the Thread Tools menu to mark the thread Resolved for yourself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width