我有一个 webview 显示一个我没有制作的网站,所以我不知道它是如何工作的细节。
在这个网站上有几个按钮可以下载各种动态生成的文件。以下是在这些按钮之一上使用的 url 请求的示例:test.example.com/Test/Export/Stuff?format=Pdf
这会导致在我的桌面浏览器和手机 chrome 上下载一个文件,但我的应用程序没有任何反应。
我在互联网上搜索了一种解决方案,但找不到有效的解决方案。
我已尝试按照此处所述设置 DownloadListener:https://forums.xamarin.com/discussion/4605/download-file-by-webview但 OnDownloadStart 永远不会触发。
我也曾尝试在我的自定义 WebViewClient 中使用 ShouldOverrideUrlLoading 拦截 url 请求,如其他帖子中所述,但没有成功。
这是按钮的 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>
我已经为 ACCESS_DOWNLOAD_MANAGER、WRITE_EXTERNAL_STORAGE 等设置了权限
谁能帮我弄清楚如何在应用程序中下载这些文件?否则我可以提供任何其他信息来帮助您吗?
最佳答案
Can anyone help me figure out how i can download these files in the app?
首先,请确保您的 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");
然后在 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();
}
关于android - Xamarin Android WebView 下载文件不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738361/