Downloading file in web view control Xamarin Forms

It seems downloading file in web view control won't work straight forward on android, same goes very well in case of iOS. If I open same URL in android browser, it works as expected.

I search for work around & got some links. Below is the brief suggestion's
1. Override the web view control & add download listener - Didn't work, the download method never got called.
2. Download event - which also didn't get called on click of particular link.

I created one sample, but not sure whether it correct way or not. Please correct me on this? (Same has to be tested on prod app yet, but it works in my sample)
additionally - we need add permission for writeAccess & internet

/// hooking on Navigating event
webView.Navigating += (object sender, WebNavigatingEventArgs e) => {

            // if url contains the .pdf 
            if (e.Url.Contains(".pdf") && Device.OS == TargetPlatform.Android)
            {
                Xamarin.Forms.DependencyService.Get<IFileDownload>().DownLoadFile(e.Url);                
            }

};

/// Android specific code DownLoadFile method

DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted);
request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, "download");
DownloadManager dm = (DownloadManager)Application.Context.GetSystemService(Application.DownloadService);
dm.Enqueue(request);

@CarlisDivyaMaloj @shivakumarhc.2864

Sign In or Register to comment.