我正在尝试使用 Xamarin 下载文件,但收到错误消息:
An exception occurred during a WebClient request. I thing that the problem is with unhautorization, but I try to download imagen for multiple web, and have the same problem.
代码:
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(
of Completed
方法中。
首先我创建一个文件夹并尝试将文件保存到其中。
当这个工作时,只需要从本地服务器下载文件。
谢谢。
最佳答案
你只创建了一个文件夹,没有创建下载文件的文件,你可以修改你的代码 webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder);
例如:
webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder + "/abc.jpg");
关于c# - 使用 Xamarin 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316882/