当多个文件上传的时候,需要依次队列形式一个个上传,当上传某个文件的时候,锁定进程,上传完毕再开启锁。
在主类中的上传按钮事件代码:
//获取openFileDialog控件选择的文件名数组(openFileDialog可多个文件选择)
{
// 把上传的文件写入流
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);
}
}