使用Xamarin下载文件 [英] Download file with Xamarin
问题描述
我正在尝试使用Xamarin下载文件,但收到错误消息:
I'm trying to download file with Xamarin, but receive an error massage:
在WebClient请求期间发生异常.我认为问题出在非授权化上,但我尝试为多个Web下载imagen,并且遇到相同的问题.
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);
}
错误消息出现在 Completed
方法的 Console.WriteLine(
中.首先,我创建了一个文件夹,然后尝试将文件保存到该文件夹中.
The error massage appears in Console.WriteLine(
of Completed
method.
Firts I create a folder and try to save file into it.
这项工作完成后,只需要本地服务器上的下载文件即可.
When this work, only need downloat file from local server.
谢谢.
推荐答案
您仅创建了一个文件夹,但没有创建用于下载文件的文件,您只需修改代码即可. webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg",文件夹);
例如这样的
You only created a folder, but didn't create a file for download file, you can just modify your code webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder);
for example like this:
webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder + "/abc.jpg");
这篇关于使用Xamarin下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!