【发布时间】:2017-09-20 08:21:10
【问题描述】:
我正在尝试使用 Xamarin 下载文件,但收到错误消息:
在 WebClient 请求期间发生异常。我认为问题在于 unhautorization,但我尝试为多个网络下载 imagen,并且遇到了同样的问题。
代码:
public void getFile() {
var pathToNewFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/CodeScanner";
Directory.CreateDirectory(pathToNewFolder);
try
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
var folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/CodeScanner";
webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder);
}
catch (Exception ex)
{
Console.WriteLine("ERROR:"+ ex.Message);
}
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("ERROR: "+ e.Error.Message);
}
错误消息出现在Console.WriteLine( 的Completed 方法中。
首先我创建一个文件夹并尝试将文件保存到其中。
当这个工作时,只需要从本地服务器下载文件。
谢谢。
【问题讨论】:
-
您是否请求了写入外部存储的权限?
-
是的@Cheesebaron
-
您可以尝试使用某种图像下载组件。你可以试试 Glide (components.xamarin.com/view/GlideComponent) 或 UrlImageViewHelper (components.xamarin.com/view/urlimageviewhelper)