WebView fails to download files from the site.

Shandera 66 Reputation points
2022-01-26T07:29:57.717+00:00

Hello. Application Xamarin Forms Android. The application has a WebView element that displays different sites. On these sites, it is possible to download files. But the file cannot be downloaded via WebView. When I click on the download button, nothing happens. Please tell me how can I fix this? Please elaborate, I'm just learning. Visual Studio 2022. Windows 10. Xamarin Forms Android.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-01-27T08:18:57.797+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You need to create a custom renderer for webview in Xamarin Android.

    Then make sure WebView has enabled javascript and the WebViewClient is set correctly.

       web.Settings.JavaScriptEnabled = true;  
       web.SetWebViewClient(new WebviewClient());  
    

    Then set a SetDownloadListener for Android.Webkit.WebView, Then use DownloadManager to download the file. Due to the Android 11 storage limitation. I store download file to the application's top-level directory with Xamarin.Essentials: File System Helpers.

       [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyWebviewCustomRenderer))]  
       namespace App11.Droid  
       {  
           internal class MyWebviewCustomRenderer : WebViewRenderer  
           {  
               public MyWebviewCustomRenderer(Context context) : base(context)  
               {  
               }  
               protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)  
               {  
                       base.OnElementChanged(e);  
                       var webView = new global::Android.Webkit.WebView(Android.App.Application.Context);  
                       webView.Settings.JavaScriptEnabled = true;  
                       webView.SetWebViewClient(new WebViewClient());  
                       webView.SetWebChromeClient(new WebChromeClient());  
                       webView.SetDownloadListener(new CustomDownloadListener());  
                       this.SetNativeControl(webView);  
                       var source = e.NewElement.Source as UrlWebViewSource;  
                       if (source != null)  
                       {  
                           webView.LoadUrl(source.Url);  
                       }  
                    
               }  
           }  
           public class CustomDownloadListener : Java.Lang.Object, IDownloadListener  
           {  
               public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)  
               {  
         
                   try  
                   {  
                       DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));  
                       request.AllowScanningByMediaScanner();  
                       request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);  
         
                       // if this path is not create, we can create it.  
                       string thmblibrary = FileSystem.AppDataDirectory + "/download";  
                       if (!Directory.Exists(thmblibrary))  
                           Directory.CreateDirectory(thmblibrary);  
         
                       request.SetDestinationInExternalFilesDir(Android.App.Application.Context, FileSystem.AppDataDirectory, "download");  
                       DownloadManager dm = (DownloadManager)Android.App.Application.Context.GetSystemService(Android.App.Application.DownloadService);  
                       dm.Enqueue(request);  
                        
                   }  
                   catch (Exception)  
                   {  
                       throw;  
                   }  
                    
               }  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Shandera 66 Reputation points
    2022-02-13T10:29:25.787+00:00

    Hello. Can I have an example code for Xamarin Android please. I can't adapt this one. You are welcome.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.