c# - 使用 Xamarin 下载文件

标签 c# android xamarin

我正在尝试使用 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/

相关文章:

c# - 在没有 IV 的情况下解密使用 AES-128 加密的 M3U8 播放列表

Android:如何创建导航栏

c# - MVVM - 单击按钮时在哪里调用函数?

c# - Xamarin.Forms 中的计时器

c# - 如何获取 UTF-16 字节数组?

c# - 设置 DateTimePicker 的默认值

c# - 跳跃比。重力

javascript - 如何使用相应的 native 应用程序从移动网站打开链接

java - 如何在 fragment 而不是 Activity 中使用 NavController(在 fragment 中带有 NavHost)?

mobile - Xamarin WebView 请求桌面站点