webclient大文件下载,怎么做?

c02645 2009-06-01 11:23:29

private void DownFile()
{
string filename = Application.StartupPath + @"\tempFile";
Directory.CreateDirectory(filename);
string TempDown = upfileUrl + "/FirefoxSetup_ZH_3.0.8.zip";
TempDown = "http://www.codeproject.com/KB/dotnet/WebClientAsyncDownloader/figure3.jpg";
if (Downloading == false) //如果无文件下载
{
long fb;
progress("正在连接服务器", 0);
Downloading = true;
try
{
WebRequest myre = WebRequest.Create(TempDown);
WebResponse mwrite = myre.GetResponse();
fb = mwrite.ContentLength;
progress("正在下载文件", 0);
pbar.Maximum = (int)fb;
//====开始下载
WebClient wc = new WebClient();
filename += @"\figure3.jpg";
if (filename != "")
{
Stream srm = wc.OpenRead(TempDown);
StreamReader srmer = new StreamReader(srm);
byte[] mbyte = new byte[fb];
int allbyte = (int)mbyte.Length;
int startbyte = 0;
while (fb > 0) //################ 循环读取文件,并显示进度.....
{
Application.DoEvents();
int m = srm.Read(mbyte, startbyte, allbyte);
if (m == 0) { break; }
startbyte += m;
allbyte -= m;
pbar.Value += m;
int a1 = (int)startbyte / 1024;
int a2 = (int)fb / 1024;
progress("正在下载文件" + a1.ToString() + "/" + a2.ToString() + "KB", 0, 0);
}

FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
fs.Write(mbyte, 0, mbyte.Length);
fs.Flush();


srm.Close();
srmer.Close();
fs.Close();

progress("正在下载文件", pbar.Maximum);
}
}
catch (WebException exp) //如地址无效或者找不到文件
{
MessageBox.Show(exp.Message, "系统 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Downloading = false;
}
else
{

MessageBox.Show("对不起,当前正在下载文件.", "系统 提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}


下载小文件没问题,下载个几M的文件就不行了,应该怎么办?
...全文
223 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧