//获取openFileDialog控件选择的文件名数组(openFileDialog可多个文件选择)privatevoidbutton1_Click(objectsender, EventArgs e)
{
label1.Text="";try{this.openFileDialog1.ShowDialog();
path=this.openFileDialog1.FileNames;//获取openFileDialog控件选择的文件名数组stringstrpath="";for(inty=0; y
{
strpath+=path[y];
}
textBox1.Text=strpath;
}catch{this.lbl_ftpStakt.Text="请选择文件!";
}
}//上传按钮事件privatevoidbutton2_Click(objectsender, EventArgs e)
{this.lbl_ftpStakt.Visible=true;//设置上传信息标签可见this.lbl_ftpStakt.Text="连接服务器...";try{for(i=0; i
{
filename=path[i].ToString();//实例化事件类myTest fo=newmyTest(filename);
fo.startUpEvent+=newmyTest.myUpEventsHandler(this.RunsOnWorkerThread);//注册事件fo.mythreadStart();//调用类的方法FileInfo p=newFileInfo(path[i].ToString());
uploadSQL(p.Name);//上传到库}//label1.Text = "上传成功";}catch{strings="";for(intx=i; x
{
FileInfo file=newFileInfo(path[i].ToString());
s+=file.Name+"";
}this.lbl_ftpStakt.Text="上传失败";
MessageBox.Show(s.ToString()+"上传失败","提示");
}
}//连接ftp上传publicvoidRunsOnWorkerThread(string_filename)
{//阻塞线程mt.WaitOne();
Interlocked.Increment(refflag);//状态值+1this.lbl_ftpStakt.Text="连接服务器中...";
FileInfo fileInf=newFileInfo(_filename);
FtpWebRequest reqFTP;//根据uri创建FtpWebRequest对象reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri("ftp://210.82.***.***/"+fileInf.Name));//ftp用户名和密码reqFTP.Credentials=newNetworkCredential("record","files");//默认为true,连接不会被关闭//在一个命令之后被执行reqFTP.KeepAlive=false;//指定执行什么命令reqFTP.Method=WebRequestMethods.Ftp.UploadFile;//指定数据传输类型reqFTP.UseBinary=true;//上传文件时通知服务器文件的大小reqFTP.ContentLength=fileInf.Length;//long _length = fileInf.Length;///缓冲大小设置为2kbintbuffLength=2048;byte[] buff=newbyte[buffLength];intcontentLen;//打开一个文件流 (System.IO.FileStream) 去读上传的文件FileStream fs=fileInf.OpenRead();try{//把上传的文件写入流Stream strm=reqFTP.GetRequestStream();//每次读文件流的2kbcontentLen=fs.Read(buff,0, buffLength);intallbye=(int)fileInf.Length;intstartbye=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 streamstrm.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(refflag);
mt.ReleaseMutex();//释放线程}