简体   繁体   English

Xamarin Android WebView下载文件什么都不做

[英]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. 我有一个webview,显示一个我没有做的网站,所以我不知道它是如何工作的细节。

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 以下是其中一个按钮上使用的网址请求示例: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. 这会导致文件在我的桌面浏览器和我的手机Chrome上下载,但我的应用程序没有任何反应。

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. 我已尝试设置DownloadListener,如下所述: https ://forums.xamarin.com/discussion/4605/download-file-by-webview但OnDownloadStart永远不会触发。

I have also tried intercepting the url request using ShouldOverrideUrlLoading in my custom WebViewClient as descibed in other posts with no luck. 我也尝试使用我的自定义WebViewClient中的ShouldOverrideUrlLoading拦截url请求,如其他帖子中所描述的那样没有运气。

Here is the html code for the button: 这是按钮的html代码:

<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. 我为ACCESS_DOWNLOAD_MANAGER,WRITE_EXTERNAL_STORAGE等设置了权限。

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: 首先,请确保您的WebView已启用javascript并正确设置了WebViewClient:

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: 然后在WebView.Download事件中使用DownloadManager下载文件:

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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