111,094
社区成员




public void UploadFile(string filename,ProgressBar progressBar)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServer + ":" + ftpPort.ToString() + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 512;
byte[] buff = new byte[buffLength];
int contentLen;
long hasRead=0;
long totalSize = fileInf.Length;
FileStream fs = fileInf.OpenRead();
Stream strm=null;
try
{
progressBar.Maximum = int.MaxValue;
progressBar.Minimum = 0;
progressBar.Value = 0;
contentLen = fs.Read(buff, 0, buffLength);
strm = reqFTP.GetRequestStream();
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
//用buffer判断进度对不对呢?
contentLen = fs.Read(buff, 0, buffLength);
hasRead += contentLen;
//请问怎么改多线程。
progressBar.Value = (int)(int.MaxValue * ((double)hasRead / (double)totalSize));
}
//strm.Close();
//fs.Close();
MessageBox.Show("Upload finished ");
progressBar.Hide();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error ");
}
finally
{
if(strm!=null)
strm.Close();
fs.Close();
}
}