Notification texts go here Contact Us Buy Now!

Download files in MAUI Android WebView

For the android, you can try to add a DownloadListener to the webview. I have testd it and the file can download successfully.

Create the custom downloadlistener class in the \Platforms\Android:


public class MyDownLoadListener : Java.Lang.Object, IDownloadListener
{
    public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
    {
        var manager = (DownloadManager)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.GetSystemService(global::Android.App.Application.DownloadService);
        var uri = global::Android.Net.Uri.Parse(url);
        DownloadManager.Request downloadrequest = new DownloadManager.Request(uri);
        downloadrequest.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
        manager.Enqueue(downloadrequest);
    }
}

In the page's xaml:


<WebView x:Name="webview" Source="https://www.myfreemp3.com.cn/" HeightRequest="500"/>

And set the listener for the webview in the Page.cs:


protected override void OnHandlerChanged()
{
    base.OnHandlerChanged();
#if ANDROID
    (webview.Handler.PlatformView as Android.Webkit.WebView).SetDownloadListener(new Platforms.Android.MyDownLoadListener());
    (webview.Handler.PlatformView as Android.Webkit.WebView).Settings.JavaScriptEnabled = true;
    (webview.Handler.PlatformView as Android.Webkit.WebView).Settings.DomStorageEnabled = true;
#endif
}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.