WebClient DownloadFileAsync()无法使用相同的URl和凭据...
有任何线索吗?
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It works fine.
client.DownloadFile(urlAddress, @"D:\1.xlsx");
}
/*using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It y creats file with 0 bytes. Dunow why is it.
client.DownloadFileAsync(new Uri(urlAddress), @"D:\1.xlsx");
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}*/
}
catch (Exception ex)
{
}
}
答案 0 :(得分:4)
您需要在异步下载完成时保持程序运行,因为它在另一个线程中运行。
尝试这样的事情,并等到它说完,然后再按Enter键结束程序:
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
client.DownloadFileAsync(new Uri(urlAddress), @"D:\1.xlsx");
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}
catch (Exception ex)
{
}
Console.ReadLine();
}
public static void Completed(object o, AsyncCompletedEventArgs args)
{
Console.WriteLine("Completed");
}
根据您使用此类型的应用程序,主线程需要在后台线程下载文件时继续运行。
答案 1 :(得分:3)
通过将主要功能声明为angular.module('MyApp',['ngMaterial', 'ngMessages'])
.controller('AppCtrl', function($scope) {
$scope.currentCard = {
redAlert: true
};
$scope.toggle = function () {
$scope.currentCard.redAlert = !$scope.currentCard.redAlert;
}
});
,您还可以将async
与DownloadFileTaskAsync
一起使用。
await