原文地址:http://blog.csdn.net/mengyao/archive/2008/06/09/2528301.aspx
因为项目要求,制作的一个多文件上传,并显示进度条一段代码(vs2005环境)。
(只为粗略的实现,代码并不规范)
当多个文件上传的时候,需要依次队列形式一个个上传,当上传某个文件的时候,锁定进程,上传完毕再开启锁。
在主类中的上传按钮事件代码:
//获取openFileDialog控件选择的文件名数组(openFileDialog可多个文件选择)
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
try
{
this.openFileDialog1.ShowDialog();
path = this.openFileDialog1.FileNames; //获取openFileDialog控件选择的文件名数组
string strpath = "";
for (int y = 0; y < path.Length; y++)
{
strpath += path[y];
}
textBox1.Text = strpath;
}
catch
{
this.lbl_ftpStakt.Text = "请选择文件!";
}
} //上传按钮事件
private void button2_Click(object sender, EventArgs e)
{
this.lbl_ftpStakt.Visible = true; //设置上传信息标签可见
this.lbl_ftpStakt.Text = "连接服务器...";
try
{
for (i = 0; i < path.Length; i++)
{
filename = path[i].ToString();
//实例化事件类
myTest fo = new myTest(filename);
fo.startUpEvent+=new myTest.myUpEventsHandler(this.RunsOnWorkerThread); //注册事件
fo.mythreadStart(); //调用类的方法
FileInfo p = new FileInfo(path[i].ToString());
uploadSQL(p.Name); //上传到库
}
//label1.Text = "上传成功";
}
catch
{
string s="";
for (int x = i; x < path.Length; x++)
{
FileInfo file = new FileInfo(path[i].ToString());
s += file.Name + " ";
}
this.lbl_ftpStakt.Text = "上传失败";
MessageBox.Show(s.ToString()+" 上传失败","提示");
}
}//连接ftp上传
public void RunsOnWorkerThread(string _filename)
{
//阻塞线程
mt.WaitOne();
Interlocked.Increment(ref flag); //状态值+1
this.lbl_ftpStakt.Text = "连接服务器中...";
FileInfo fileInf = new FileInfo(_filename);
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://210.82.***.***/" + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential("record", "files");
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
//long _length = fileInf.Length; /////////
// 缓冲大小设置为2kb
int buffLength = 2048; ////
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
this.myProgressControl.Maximum = allbye;
this.myProgressControl.Minimum = 0;
this.myProgressControl.Visible = true;
this.lbl_ftpStakt.Visible = true;
this.lbl_ftpStakt.Text = "服务器连接中...";
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
this.lbl_ftpStakt.Text = "已上传:" + (int)(startbye / 1024) + "KB/" + "总长度:" + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + fileInf.Name;
myProgressControl.Value = startbye;
}
// 关闭两个流
strm.Close();
fs.Close();
this.myProgressControl.Visible = false;
this.lbl_ftpStakt.Text = "上传成功!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
Interlocked.Decrement(ref flag);
mt.ReleaseMutex();//释放线程
}
处理上传线程的委托事件类
/// <summary>
/// 委托事件类
/// </summary>
class myTest
{
public string filename;
public delegate void myUpEventsHandler(string _filename);
public event myUpEventsHandler startUpEvent;
public myTest()
{
}
/// <summary>
///
/// </summary>
/// <param name="_filename">上传的文件名</param>
public myTest(string _filename)
{
this.filename = _filename;
}
/// <summary>
/// 开始一个线程,执行事件
/// </summary>
public void mythreadStart()
{
Thread thr = new Thread(new ThreadStart(this.mystart));
thr.Start();
}
/// <summary>
/// 开始事件
/// </summary>
public void mystart()
{
startUpEvent(this.filename);
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mengyao/archive/2008/06/09/2528301.aspx