ftp上传文件显示进度条php,ftp上传文件 用进度条progressBar显示进度()

当前位置:我的异常网» C# » ftp上传文件 用进度条progressBar显示进度()

ftp上传文件 用进度条progressBar显示进度()

www.myexceptions.net  网友分享于:2013-03-30  浏览:513次

ftp上传文件 用进度条progressBar显示进度(在线等)

public class upload : Mypublic

{

public delegate void delProgress(int value);

public event delProgress eventProgress;

public delegate void delFinish();

public event delFinish eventFinish;

public string fileName = "";

private long current = 0;

private long total = 1;

private int percent = 0;

int temp = 0;

public void Upload()

{

FileInfo fileinfo = new FileInfo(fileName);

string uri = "ftp://" + IpAddress + "/" + fileinfo.Name;

FtpWebRequest reqFtp;

reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IpAddress + "/" + fileinfo.Name));

reqFtp.Credentials = new NetworkCredential(Userid, Password);

reqFtp.KeepAlive = false;

reqFtp.Method = WebRequestMethods.Ftp.UploadFile;

reqFtp.UseBinary = true;

reqFtp.ContentLength = fileinfo.Length;

total = fileinfo.Length;

int bufferlenth = 2048;

byte[] buff = new byte[bufferlenth];

int contentLen;

FileStream fs = fileinfo.OpenRead();

try

{

Stream strm = reqFtp.GetRequestStream();

contentLen = fs.Read(buff, 0, bufferlenth);

while (contentLen != 0)

{

strm.Write(buff, 0, bufferlenth);

contentLen = fs.Read(buff, 0, bufferlenth);

current += contentLen;

temp = Int32.Parse((100 * current / total).ToString());

if (temp != percent)

{

eventProgress(temp);

percent = temp;

} }

strm.Close();

fs.Close();

eventFinish();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "Upload Error");

}

}

}

按钮事件

private void button1_Click(object sender, EventArgs e)

{

if (DialogResult.OK == this.openFileDialog1.ShowDialog())

{

upload up = new upload();

up.IpAddress = "192.168.1.31";

up.Userid = "Administrator";

up.Password = "110";

up.fileName = this.openFileDialog1.FileName;

up.eventProgress += new upload.delProgress(up_eventProgress);

up.eventFinish += new upload.delFinish(up_eventFinish);

Thread t = new Thread(new ThreadStart(up.Upload));

t.Start();

}

}

void up_eventFinish()

{

up_eventProgress(100);

MessageBox.Show("上传成功!");

}

void up_eventProgress(int value)

{

this.progressBar1.Value = value;

this.label1.Text = value.ToString() + "%"; }

运行之后报错 :线程间操作无效:从不是创建控件ProgressBar1的线程访问它

请教高手!在线等~~

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值