简体   繁体   中英

Xamarin Android WebView download file does nothing

I have a webview that shows a website which i did not make so i dont know the details of how it works.

On this site there are several buttons which download various files which are generated on the fly. Here is an example of the url request used on one of these buttons: test.example.com/Test/Export/Stuff?format=Pdf

This causes a file to be downloaded on my desktop browser and on my phones chrome but nothing happens on my app.

I have scoured the internet searching for a solution but i am unable to find one that works.

I have tried setting DownloadListener as discribed here: https://forums.xamarin.com/discussion/4605/download-file-by-webview but the OnDownloadStart never triggers.

I have also tried intercepting the url request using ShouldOverrideUrlLoading in my custom WebViewClient as descibed in other posts with no luck.

Here is the html code for the button:

<input id="exportPdfButton" class="secondary hover" format="Pdf" value="Download (PDF)" name="exportPdfButton" type="submit">
<script id="dxss_848651839" type="text/javascript">

<!--

var dxo = new MVCxClientButton('exportPdfButton');
dxo.InitGlobalVariable('exportPdfButton');
dxo.SetProperties({'autoPostBack':true,'isNative':true});
dxo.SetEvents({'Click':ExportButtonOnClick});
dxo.AfterCreate();

//-->
</script>

I have set permissions for ACCESS_DOWNLOAD_MANAGER, WRITE_EXTERNAL_STORAGE etc.

Can anyone help me figure out how i can download these files in the app? Otherwise is there any other information i can provide to help?

Can anyone help me figure out how i can download these files in the app?

Firstly, please make sure your WebView has enabled javascript and the WebViewClient is set correctly:

mWebview = FindViewById<WebView>(Resource.Id.mWebview);
mWebview.Download += MWebview_Download;
var client = new WebViewClient();
mWebview.Settings.JavaScriptEnabled = true;
mWebview.SetWebViewClient(client);

mWebview.LoadUrl("your url");

Then in the WebView.Download event use DownloadManager to download the file:

private void MWebview_Download(object sender, DownloadEventArgs e)
{
    var url = e.Url;
    DownloadManager.Request request = new DownloadManager.Request(Uri.Parse(url));

    request.AllowScanningByMediaScanner();
    request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted); //Notify client once download is completed!
    request.SetDestinationInExternalPublicDir(Environment.DirectoryDownloads, "CPPPrimer");
    DownloadManager dm = (DownloadManager)GetSystemService("download");
    dm.Enqueue(request);
    Toast.MakeText(ApplicationContext, "Downloading File",ToastLength.Long//To notify the Client that the file is being downloaded
                ).Show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM