好的,当我使用DownloadDataAsync并让它将字节返回给我的时候,我遇到了一个问题。这是我正在使用的代码:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes;
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
bytes = client.DownloadDataAsync(new Uri("http://example.net/file.exe"));
}
}
void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
label1.Text = Math.Round(bytesIn / 1000) + " / " + Math.Round(totalBytes / 1000);
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
if (progressBar1.Value == 100)
{
MessageBox.Show("Download Completed");
button2.Enabled = true;
}
}
我收到的错误是“无法隐式将类型'void'转换为'byte []'”。有没有办法让它成为可能,并在下载完成后给我字节?如果删除“bytes =”,它可以正常工作。