使用C#Windows窗体和webclient下载文件
我正在学习如何在C#windows窗体中使用http请求和webclient。目前,我已经从中获得了以下代码,我正在努力使其工作以及理解它 代码成功执行并显示messagebox“Download Complete”框,但实际上并没有下载文件。有人能告诉我这是怎么回事,我做错了什么吗使用C#Windows窗体和webclient下载文件,c#,winforms,web,download,C#,Winforms,Web,Download,我正在学习如何在C#windows窗体中使用http请求和webclient。目前,我已经从中获得了以下代码,我正在努力使其工作以及理解它 代码成功执行并显示messagebox“Download Complete”框,但实际上并没有下载文件。有人能告诉我这是怎么回事,我做错了什么吗 private void btnDownload_Click(object sender, EventArgs e) { string filepath = txtBxSaveTo.
private void btnDownload_Click(object sender, EventArgs e)
{
string filepath = txtBxSaveTo.Text.ToString();
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://download.thinkbroadband.com/10MB.zip"), filepath);
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
private void btnSavetoLocation_Click(object sender, EventArgs e)
{
FolderBrowserDialog selectedFolder = new FolderBrowserDialog();
if (selectedFolder.ShowDialog() == DialogResult.OK)
{
txtBxSaveTo.Text = selectedFolder.SelectedPath;
}
}
}
}下载文件方法引发异常。也。。。在转到异步之前,请先使用非异步版本
{“远程服务器返回错误:(407)需要代理身份验证。”}下载文件方法引发异常。也。。。在转到异步之前,请先使用非异步版本
{“远程服务器返回了一个错误:(407)需要代理身份验证。”}在这种情况下,如果您只想下载文件,可能需要进行同步下载调用,而不是像您尝试实现的那样进行异步调用 这可以通过与以下类似的方式完成:
private void btnDownload_Click(object sender, EventArgs e)
{
string filepath = txtBxSaveTo.Text.ToString();
WebClient webClient = new WebClient();
webClient.DownloadFile("http://download.thinkbroadband.com/10MB.zip", filepath);
}
这将锁定程序,直到文件下载或出现错误,但您是否特别需要异步下载?如果您这样做,以下解释可能会有所帮助:
private void btnDownload_Click(object sender, EventArgs e)
{
//Retrieve the path from the input textbox
string filepath = txtBxSaveTo.Text.ToString();
//Create a WebClient to use as our download proxy for the program.
WebClient webClient = new WebClient();
//Attach the DownloadFileCompleted event to your new AsyncCompletedEventHandler Completed
//so when the event occurs the method is called.
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//Attach the DownloadProgressChanged event to your DownloadProgressChangedEventHandler ProgressChanged,
//so again when the event occurs the method is called.
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
//Attempt to actually download the file, this is where the error that you
//won't see is probably occurring, this is because it checks the url in
//the blocking function internally and won't execute the download itself
//until this clears.
webClient.DownloadFileAsync(new Uri("http://example.com/myfile.txt"), filepath);
}
//Method that just increments the progressBar every time the DownloadProgressChangedEvent from webClient fires.
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
//This is your method that will pop when the AsyncCompletedEvent is fired,
//this doesn't mean that the download was successful though which is why
//it's misleading, it just means that the Async process completed.
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
就个人而言,我会在这种情况下使用同步调用,直到您更好地了解异步调用及其优缺点。对于这种情况,如果您只想下载文件,您可能希望进行同步下载调用,而不是像您尝试实现的那样进行异步调用 这可以通过与以下类似的方式完成:
private void btnDownload_Click(object sender, EventArgs e)
{
string filepath = txtBxSaveTo.Text.ToString();
WebClient webClient = new WebClient();
webClient.DownloadFile("http://download.thinkbroadband.com/10MB.zip", filepath);
}
这将锁定程序,直到文件下载或出现错误,但您是否特别需要异步下载?如果您这样做,以下解释可能会有所帮助:
private void btnDownload_Click(object sender, EventArgs e)
{
//Retrieve the path from the input textbox
string filepath = txtBxSaveTo.Text.ToString();
//Create a WebClient to use as our download proxy for the program.
WebClient webClient = new WebClient();
//Attach the DownloadFileCompleted event to your new AsyncCompletedEventHandler Completed
//so when the event occurs the method is called.
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//Attach the DownloadProgressChanged event to your DownloadProgressChangedEventHandler ProgressChanged,
//so again when the event occurs the method is called.
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
//Attempt to actually download the file, this is where the error that you
//won't see is probably occurring, this is because it checks the url in
//the blocking function internally and won't execute the download itself
//until this clears.
webClient.DownloadFileAsync(new Uri("http://example.com/myfile.txt"), filepath);
}
//Method that just increments the progressBar every time the DownloadProgressChangedEvent from webClient fires.
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
//This is your method that will pop when the AsyncCompletedEvent is fired,
//this doesn't mean that the download was successful though which is why
//it's misleading, it just means that the Async process completed.
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
就我个人而言,在这种情况下,我会使用同步调用,直到您更好地了解异步调用以及它们之间的优缺点。在
已完成的
处理程序中,尝试在已完成的
处理程序中检查e.Error
和e.Cancelled
,尝试检查e.Error
和e.Cancelled
极好的解释。谢谢直到我在文件路径字符串中放置了一个文件名,我才开始使用它。是否有一种方法可以使下载的文件自动命名为与服务器上的文件相同的名称?是否有原因不能以如下方式对文件名进行注释:txtBxSaveTo.Text.ToString()+“10MB.zip”
?我很好奇,因为您似乎正在对服务器上的文件路径进行硬编码。我不知道这一点,只是在示例中包含这一点似乎不谨慎,因为这似乎与原始问题无关。非常好的解释。谢谢直到我在文件路径字符串中放置了一个文件名,我才开始使用它。是否有一种方法可以使下载的文件自动命名为与服务器上的文件相同的名称?是否有原因不能以如下方式对文件名进行注释:txtBxSaveTo.Text.ToString()+“10MB.zip”
?我很好奇,因为您似乎正在对服务器上的文件路径进行硬编码。我不知道这一点,只是在示例中包含这一点似乎不谨慎,因为这似乎与原始问题无关。这与我遇到的问题相同。有没有什么线索可以解决这个问题?这和我遇到的问题是一样的。有什么线索可以让你过去吗?