下面程序代码的功能是:从服务器下载文件
但是不知道为什么每次下载最多只能下96KB左右,
如果比96KB大的文件不是少了就是打不开 很郁闷 高手帮帮忙看看啊using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; //包含FileStream类 文件
using System.Net; //WebClient类处于System.Net名字空间中
using System.Threading; //包含Thread类 线程namespace downloadfire
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} WebClient client = new WebClient(); private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
} x private void button3_Click(object sender, EventArgs e)
{
this.client.CancelAsync();
this.client.Dispose();
} private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(this.StartDownload));
th.Start();
}
private void StartDownload()
{
Control.CheckForIllegalCrossThreadCalls=false;
button1.Enabled = false;
string URL = textBox1.Text;
int n = URL.LastIndexOf('/');
int n2=URL.Length;
string URLAddress = URL.Substring(0, n2);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = textBox2.Text;
string Path = Dir + '\\' + fileName;
try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
}
try
{
toolStripStatusLabel2.Text= "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
toolStripStatusLabel2.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(); toolStripStatusLabel2.Text = "下载完毕!";
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
textBox2.Text = "";
} button1.Enabled = true;
} private void Form1_Load(object sender, EventArgs e)
{
} private void button4_Click(object sender, EventArgs e)
{
FolderBrowserDialog openFolderDialog1=new FolderBrowserDialog();
openFolderDialog1.RootFolder=Environment.SpecialFolder.MyComputer;
if(openFolderDialog1.ShowDialog()==DialogResult.OK)
textBox2.Text=openFolderDialog1.SelectedPath;
}
}
}
但是不知道为什么每次下载最多只能下96KB左右,
如果比96KB大的文件不是少了就是打不开 很郁闷 高手帮帮忙看看啊using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; //包含FileStream类 文件
using System.Net; //WebClient类处于System.Net名字空间中
using System.Threading; //包含Thread类 线程namespace downloadfire
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} WebClient client = new WebClient(); private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
} x private void button3_Click(object sender, EventArgs e)
{
this.client.CancelAsync();
this.client.Dispose();
} private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(this.StartDownload));
th.Start();
}
private void StartDownload()
{
Control.CheckForIllegalCrossThreadCalls=false;
button1.Enabled = false;
string URL = textBox1.Text;
int n = URL.LastIndexOf('/');
int n2=URL.Length;
string URLAddress = URL.Substring(0, n2);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = textBox2.Text;
string Path = Dir + '\\' + fileName;
try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
}
try
{
toolStripStatusLabel2.Text= "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
toolStripStatusLabel2.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(); toolStripStatusLabel2.Text = "下载完毕!";
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
textBox2.Text = "";
} button1.Enabled = true;
} private void Form1_Load(object sender, EventArgs e)
{
} private void button4_Click(object sender, EventArgs e)
{
FolderBrowserDialog openFolderDialog1=new FolderBrowserDialog();
openFolderDialog1.RootFolder=Environment.SpecialFolder.MyComputer;
if(openFolderDialog1.ShowDialog()==DialogResult.OK)
textBox2.Text=openFolderDialog1.SelectedPath;
}
}
}
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break; startmbyte += m;
allmybyte -= m;
}
byte[] mbyte = new byte[str.Length];
大文件好像是用stream流下的 不过我不会写哦 能教教吗
文件已经下载到本地啦,本地文件就是第二个参数指定的文件所以搂主这一句代码:client.DownloadFile(URLAddress, fileName); 之后的代码其实是多余的,不妨去掉看看也就是改成:
try
{
toolStripStatusLabel2.Text= "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
toolStripStatusLabel2.Text = "下载完毕!";
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
textBox2.Text = "";
} button1.Enabled = true;
} 就可以了应该。
楼主已经写死了啊,不能接收超过10万字节大小的文件。
应该增加一个方法,先取得要下载的文件大小,然后再分配字节
long size = client.GetFileSize(URLAddress);
byte[] mbyte = new byte[size];
不知道楼主是不是用的WCF服务。在WCF中直接获取从服务端返回的Stream的Length是不支持的,会抛出NotSupported异常。
而FTP下载,如果直接使用.NET提供的方法的话,一般使用System.Net.FtpWebResquest和FtpWebResponse这两个专业的FTP类