流下载文件
时间:2011-01-04 来源:共谱一曲
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();
Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream(LocalPath, FileMode.OpenOrCreate, FileAccess.Write);
// FileStream f = new FileStream(LocalPath,FileMode.OpenOrCreate,FileAccess.Write);
long length = wr.ContentLength;
long i = 0;
decimal j = 0;
while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
if ((i % 1024) == 0)
{
j = Math.Round(Convert.ToDecimal((Convert.ToDouble(i) / Convert.ToDouble(length)) * 100), 4);
// statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节 下载进度" + j.ToString() + "%";
}
else
{
// statusBar1.Text = "当前下载文件大小:" + length.ToString() + "字节 当前下载大小:" + i + "字节";
}
}
sIn.Close();
wr.Close();
fs.Close();
return true;
}
catch(Exception ex) { MessageBox.Show(ex.Message); return false; }
}