从网上找了一个例子,是通过webclient从服务器上下载文件的。试过以后可以下载文件,但文件大小不一样(比如我的文件有3M大,下来以后就1K多..),内容也乱了。..
请问为什么啊?有什么办法能提高精度吗?百分百下载!!例子主函数:
private void StartDownload()
{
// Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0, n);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = tarAddress.Text;
string Path = Dir + '\\' + fileName; try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
} try
{
// statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[10000000];
int allmybyte = (int)mbyte.Length;
MessageBox.Show(allmybyte.ToString()); //test int startmbyte = 0;
// statusBar.Text = "正在接收数据...";
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break; startmbyte += m;
allmybyte -= m;
} FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
str.Close();
fstr.Close(); // statusBar.Text = "下载完毕!";
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
// statusBar.Text = "";
} Start.Enabled = true;
}
private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
请问为什么啊?有什么办法能提高精度吗?百分百下载!!例子主函数:
private void StartDownload()
{
// Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0, n);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = tarAddress.Text;
string Path = Dir + '\\' + fileName; try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
} try
{
// statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[10000000];
int allmybyte = (int)mbyte.Length;
MessageBox.Show(allmybyte.ToString()); //test int startmbyte = 0;
// statusBar.Text = "正在接收数据...";
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break; startmbyte += m;
allmybyte -= m;
} FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
str.Close();
fstr.Close(); // statusBar.Text = "下载完毕!";
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
// statusBar.Text = "";
} Start.Enabled = true;
}
private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
byte[] bt = new byte[1024];
MemoryStream ms = new MemoryStream();
int count = stm.Read(bt, 0, 1024);
while (count > 0)
{
ms.Write(bt, 0, count);
count = stm.Read(bt, 0, 1024);
}
res.Close();
req.Abort();
ms.Position = 0;
byte[] bt1 = new byte[(int)ms.Length];
ms.Read(bt1, 0, (int)ms.Length);
stm.Close();
ms.Close();
FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(bt1, 0, bt1.length);