问题描述
我很难从URL下载一些MB excel文件,然后使用它.我正在使用VS2010,所以我不能使用await关键字.我的代码如下:
I have struggle with downloading few MB excel file from URL and then work with it. Im using VS2010 so i cant use await keyword.My code follows:
using (WebClient webClient = new WebClient())
{
// setting Windows Authentication
webClient.UseDefaultCredentials = true;
// event fired ExcelToCsv after file is downloaded
webClient.DownloadFileCompleted += (sender, e) => ExcelToCsv(fileName);
// start download
webClient.DownloadFileAsync(new Uri("http://serverx/something/Export.ashx"), exportPath);
}
ExcelToCsv()
方法中的行
using (FileStream stream = new FileStream(filePath, FileMode.Open))
向我抛出错误:
我仅尝试了webClient.DownloadFile()
而不发生任何事件,但是它引发了相同的错误.如果我也不要处置,则会引发相同的错误.我该怎么办?
I tried webClient.DownloadFile()
only without an event but it throws same error. Same error is throwed if i do not dispose too. What can i do ?
临时解决方法可能是Sleep()
方法,但不是防弹措施.
Temporary workaround may be Sleep()
method but its not bullet proof.
谢谢
我尝试了标准处理的第二种方法,但是代码有误
I tried second approach with standard handling but i have mistake in the code
using (WebClient webClient = new WebClient())
{
// nastaveni ze webClient ma pouzit Windows Authentication
webClient.UseDefaultCredentials = true;
// <--- I HAVE CONVERT ASYNC ERROR IN THIS LINE
webClient.DownloadFileCompleted += new DownloadDataCompletedEventHandler(HandleDownloadDataCompleted);
// spusteni stahovani
webClient.DownloadFile(new Uri("http://czprga2001/Logio_ZelenyKyblik/Export.ashx"), TempDirectory + PSFileName);
}
public delegate void DownloadDataCompletedEventHandler(string fileName);
public event DownloadDataCompletedEventHandler DownloadDataCompleted;
static void HandleDownloadDataCompleted(string fileName)
{
ExcelToCsv(fileName);
}
方法3我尝试了这段代码
approach 3I tried this code
while (true)
{
if (isFileLocked(downloadedFile))
{
System.Threading.Thread.Sleep(5000); //wait 5s
ExcelToCsv(fileName);
break;
}
}
似乎永远无法访问:/我不明白.
and it seems that it is never accessible :/ I dont get it.
推荐答案
尝试使用DownloadFile
而不是DownloadFileAsync
,就像在Edit 1中一样:
Try to use DownloadFile
instead of DownloadFileAsync
, as you do in Edit 1, like this:
string filename=Path.Combine(TempDirectory, PSFileName);
using (WebClient webClient = new WebClient())
{
// nastaveni ze webClient ma pouzit Windows Authentication
webClient.UseDefaultCredentials = true;
// spusteni stahovani
webClient.DownloadFile(new Uri("http://czprga2001/Logio_ZelenyKyblik/Export.ashx"), filename);
}
ExcelToCsv(filename); //No need to create the event handler if it is not async
在您的示例中,您似乎不需要异步下载,因此请使用同步下载,并避免可能的相关问题,例如此处.
From your example it seems that you do not need asynchronous download, so use synchronous download and avoid possible related problems like here.
还可以使用Path.Combine
组合路径的一部分,例如文件夹和文件名.
Also use Path.Combine
to combine parts of a path like folder and filename.
也有可能它被其他东西锁定,请使用Sysinternals Process Explorer的Find DLL或Handle函数进行检查.
There is also a chance that it is locked by something else, use Sysinternals Process Explorer's Find DLL or Handle function to check it.
使用本地磁盘存储下载的文件,以防止网络出现问题.
Use local disk to store downloaded file to prevent problems with network.
这篇关于等待直到通过webClient从URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!