//獲取openFileDialog控件選擇的文件名數(shù)組(openFileDialog可多個(gè)文件選擇)
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
try
{
this.openFileDialog1.ShowDialog();
path = this.openFileDialog1.FileNames; //獲取openFileDialog控件選擇的文件名數(shù)組
string strpath = "";
for (int y = 0; y < path.Length; y++)
{
strpath += path[y];
}
textBox1.Text = strpath;
}
catch
{
this.lbl_ftpStakt.Text = "請(qǐng)選擇文件!";
}
}
//上傳按鈕事件
private void button2_Click(object sender, EventArgs e)
{
this.lbl_ftpStakt.Visible = true; //設(shè)置上傳信息標(biāo)簽可見(jiàn)
this.lbl_ftpStakt.Text = "連接服務(wù)器...";
try
{
for (i = 0; i < path.Length; i++)
{
filename = path[i].ToString();
//實(shí)例化事件類(lèi)
myTest fo = new myTest(filename);
fo.startUpEvent+=new myTest.myUpEventsHandler(this.RunsOnWorkerThread); //注冊(cè)事件
fo.mythreadStart(); //調(diào)用類(lèi)的方法
FileInfo p = new FileInfo(path[i].ToString());
uploadSQL(p.Name); //上傳到庫(kù)
}
//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)
{
//阻塞線(xiàn)程
mt.WaitOne();
Interlocked.Increment(ref flag); //狀態(tài)值+1
this.lbl_ftpStakt.Text = "連接服務(wù)器中...";
FileInfo fileInf = new FileInfo(_filename);
FtpWebRequest reqFTP;
// 根據(jù)uri創(chuàng)建FtpWebRequest對(duì)象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://210.82.***.***/" + fileInf.Name));
// ftp用戶(hù)名和密碼
reqFTP.Credentials = new NetworkCredential("record", "files");
// 默認(rèn)為true,連接不會(huì)被關(guān)閉
// 在一個(gè)命令之后被執(zhí)行
reqFTP.KeepAlive = false;
// 指定執(zhí)行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定數(shù)據(jù)傳輸類(lèi)型
reqFTP.UseBinary = true;
// 上傳文件時(shí)通知服務(wù)器文件的大小
reqFTP.ContentLength = fileInf.Length;
//long _length = fileInf.Length; /////////
// 緩沖大小設(shè)置為2kb
int buffLength = 2048; ////
byte[] buff = new byte[buffLength];
int contentLen;
// 打開(kāi)一個(gè)文件流 (System.IO.FileStream) 去讀上傳的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上傳的文件寫(xiě)入流
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 = "服務(wù)器連接中...";
// 流內(nèi)容沒(méi)有結(jié)束
while (contentLen != 0)
{
// 把內(nèi)容從file stream 寫(xiě)入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
this.lbl_ftpStakt.Text = "已上傳:" + (int)(startbye / 1024) + "KB/" + "總長(zhǎng)度:" + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + fileInf.Name;
myProgressControl.Value = startbye;
}
// 關(guān)閉兩個(gè)流
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();//釋放線(xiàn)程
}
/// <summary>
/// 委托事件類(lèi)
/// </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>
/// 開(kāi)始一個(gè)線(xiàn)程,執(zhí)行事件
/// </summary>
public void mythreadStart()
{
Thread thr = new Thread(new ThreadStart(this.mystart));
thr.Start();
}
/// <summary>
/// 開(kāi)始事件
/// </summary>
public void mystart()
{
startUpEvent(this.filename);
}
}