简体   繁体   English

使用C#Windows Forms和WebClient下载文件

[英]Download files with C# Windows Forms and webclient

I am learning how to use http requests and webclient in C# windows forms. 我正在学习如何在C#Windows窗体中使用http请求和webclient。 Currently I have gotten the following code from Example and I am trying to make it work as well as understand it. 目前,我已经从Example中获得了以下代码,并且我试图使它工作并理解它。

The code executes successfully and displays the messagebox "Download Complete" box but it does not actually download the file. 该代码成功执行并显示消息框“下载完成”框,但实际上并未下载文件。 Would someone explain to me how this works and what I am doing wrong? 有人会告诉我这是怎么回事以及我在做什么错吗?

    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;

        }
    }
}

} }

For this case, if you just want to download the file, you may want to make a Synchronously download call instead of an Asynchronously call like you are trying to implement. 对于这种情况,如果您只想下载文件,则可能要进行同步下载调用,而不要像尝试实现的那样进行异步调用。

This can be done in a manner similar to this: 这可以通过类似于以下方式的方式来完成:

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);
}

This will lock the program until the file is downloaded or an error occurs, but do you have a particular need to do this download Asynchronously? 这将锁定程序,直到下载文件或发生错误为止,但是您是否特别需要异步进行此下载? If you do then the following explanation might be of some use: 如果您这样做,则以下解释可能有用:

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!");
}

Personally I would use a Synchronous call in this case until you get a better understand of Asynchronous calls and the pros and cons between them. 就我个人而言,在这种情况下,我将使用同步调用,直到您更好地了解了异步调用及其之间的优缺点。

The download file method is throwing an exception. 下载文件方法引发异常。 Also ... use the non-async versions to start out with before going to async. 另外...在开始异步之前,先使用非异步版本开始。

{"The remote server returned an error: (407) Proxy Authentication Required."} {“远程服务器返回错误:(407)必需的代理身份验证。”}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM