如何在C#中上传文件ftp时显示进度条?需要帮助请
问题描述:
protected void btnsave_Click(object sender, EventArgs e)
{
using (SqlConnection condatabase = new SqlConnection(constr))
{
string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";
string ftpUser = "sa";
string ftpPassWord = "111";
Stream requestStream = null;
Stream fileStream = null;
FtpWebResponse uploadResponse = null;
if (FileUpload.PostedFile != null)
{
HttpPostedFile uploadFile = FileUpload.PostedFile;
if (System.IO.Path.GetExtension(uploadFile.FileName).ToLower() == ".jpg")
{
string strFileName = Path.GetFileName(uploadFile.FileName);
string a = ("../musicnote123/uploadpic/" + strFileName);
int FileLength = FileUpload.PostedFile.ContentLength;
if (FileLength < 512 * 1024 * 1024)
{
try
{
Uri uri = new Uri(FileSaveUri + Path.GetFileName(FileUpload.PostedFile.FileName));
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
requestStream = uploadRequest.GetRequestStream();
byte[] buffer = new byte[FileLength];
fileStream = FileUpload.PostedFile.InputStream;
fileStream.Read(buffer, 0, FileLength);
requestStream.Write(buffer, 0, FileLength);
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
uploadFile.SaveAs(MapPath(a));
SqlCommand cmdsave;
string sqlsave = "Update memberlist set member_pic=@mpic Where member_id='" + Convert.ToString(Session["username"]) + "'";
cmdsave = new SqlCommand(sqlsave, condatabase);
cmdsave.Parameters.Add(new SqlParameter("@mpic", a));
condatabase.Open();
cmdsave.ExecuteNonQuery();
condatabase.Close();
}
catch (Exception ex)
{
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}
else {}
}
else {}
else {}
}
}
答
添加一个后台工作者,你的代码应该(或可能)最终看起来像这样:
Add a Background Worker and your code should (or could) end up looking something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Do NOT (ever) refer to form controls from this method
//Keep it self contained
//Add your background code
//In your case it's your "btnsave_Click" method code, just check you aren't referring back to objects owned by the form thread:
//using (SqlConnection condatabase = new SqlConnection(constr))
//{
//string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";
//string ftpUser = "sa";
//string ftpPassWord = "111";
//Stream requestStream = null;
//Stream fileStream = null;
//FtpWebResponse uploadResponse = null;
//if (FileUpload.PostedFile != null)
//{
//etc.
//As you pass through each loop in your long runtime method do something like this
backgroundWorker1.ReportProgress(Convert.ToInt32((countsofar / totalofthingstodo) * 100));
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Only ever refer to form controls from this method
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = progressBar1.Minimum;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
}
为了使上述工作,BackgroundWorker必须将WorkerReportsProgress设置为True。
如果您无法计算百分比进度,那么将progressBar设为Marquee(Style = Marquee)并且不报告进度,只需将其默认为禁用(启用) = False),在作业开始时启用它并在 backgroundWorker1_RunWorkerCompleted
方法中禁用它。
Mike
For the above to work the BackgroundWorker must have WorkerReportsProgress set to True.
If you can''t calculate the percentage progress then make the progressBar a Marquee (Style = Marquee) and don''t report progress, just have it default to disabled (Enabled = False), enable it at the start of the job to be done and disable it in the backgroundWorker1_RunWorkerCompleted
method.
Mike