如何使用 webview 在 xamarin 中下载文件? [英] How can I download file in xamarin with webview?

查看:77
本文介绍了如何使用 webview 在 xamarin 中下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin 开发的新手.我希望你能以简单的方式解决我的问题.我有一个用 Xamarin 开发的跨平台应用程序,我想从 url 下载文件.

I'm new in a Xamarin development. I hope that you can resolve my problem in a simple mode. I've a crossplatform app developed with Xamarin and I want to download a file from a url.

我尝试使用 C# 代码下载文件,但我不能这样做(对于跨平台应用程序),所以我想尝试其他方式.我想使用网络视图并将文件直接下载到网络视图中.问题是,当我在 webview 中单击下载"按钮时,它会执行任何操作.你能用简单的方式帮助我吗?

I tried to download file using C# code but i can't do it (for crossplatform app) so I want to try other way. I want to use a webview and download the file directly into the web view. the problem is that when i click the button "download" into the webview it do anything. can you help me in a simply way?

我的网络视图是这样的:

<WebView Source="https://download.cnet.com/Free-YouTube-Downloader/3001-2071_4-75219434.html/" x:Name="webv" WidthRequest="1000" HeightRequest="1000" />

这是一个url的例子

推荐答案

您有很多选择.但这里有 2 个.很简单.

You have some plenty choices. But here's 2 of them. It's a simple one.

  1. 使用 Device.OpenUri(new Uri("Download link here")).这个方法很简单.它在客户端移动 Web 浏览器中打开 URL.然后,网络浏览器将处理后台工作程序以获取下载进度.

  1. Using Device.OpenUri(new Uri("Download link here")). This method is simple. It open the URL in client mobile web browser. And from there, the web browser will handle the background worker for download progress.

使用 WebClient() 类.使用这个类,你可以得到你喜欢的数据类型的响应,可能是 stringbyte[].以下是示例:

Using WebClient() class. Using this class, you'll can have your response in data type that you like maybe string or byte[]. Below's the sample:

var wb = new WebClient();

//Download as string
string x = wb.DownloadString("Download url");

//Download as byte[]
byte[] x = wb.DownloadData("Download Url");

x的返回值,现在可以使用System.IO改写到另一个文件中.

From the return value of x, now you can rewrite it using System.IO to another file.

注意:这些方法只是基本的.您可以在任务中实现它,因此您的应用在下载过程中不会挂起.

Note: These method is just basic. You can implement it in task, so your app will not hanging up while the download is in process.

更好的方法:通过依赖服务,从原生 Android & 下载文件iOS 本身通过接口类.你可以看看这个:https://github.com/SimonSimCity/Xamarin-CrossDownloadManager#where-are-the-files-stored

Better approach: Via dependency service, download file from the native Android & iOS itself via interface class. you can check this one out: https://github.com/SimonSimCity/Xamarin-CrossDownloadManager#where-are-the-files-stored

这篇关于如何使用 webview 在 xamarin 中下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆