问高人一个关于进度条的问题:
我有如下一段代码,就是通过FTP方式向一个机器传电影,现在加了一个进度条,但是进度条的值一直不能满,代码如下:
 private void FtpUpFile()
        {
            DataRow[] drAry = dtMMS.Select("mms_id='" + cbMMSServer.SelectedValue + "'");            FileInfo fileInf = new FileInfo(strFileName);
            FtpWebRequest reqFTP;
            // 根据uri创建FtpWebRequest对象 
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(drAry[0]["strFTPAddress"].ToString() + fileInf.Name));
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(drAry[0]["strFTPAccount"].ToString(), drAry[0]["strFTPPassword"].ToString());
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
            reqFTP.KeepAlive = false;
            // 指定执行什么命令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // 指定数据传输类型
            reqFTP.UseBinary = true;
            // 上传文件时通知服务器文件的大小
            reqFTP.ContentLength = fileInf.Length;
            // 缓冲大小设置为2kb
            int buffLength = 2048;  
            byte[] buff = new byte[buffLength];
            int contentLen;
            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
            FileStream fs = fileInf.OpenRead();            this.progressBar.Value = 0;
            this.lalSuccess.Visible = false;
            if ((int)(fs.Length / 1024) == 0)
            {
                //_progSend.Maximum = 1;
                //_progSend.Value = 1;
                this.progressBar.Maximum = (int)(fs.Length / 1024);
                //this.progressBar.Maximum = 1;
                this.progressBar.Value = 1;
            }
            else
                //_progSend.Maximum = (int)(read.Length / 1024);
                this.progressBar.Maximum = (int)(fs.Length / 1024);
            try
            {
                // 把上传的文件写入流
                Stream strm = reqFTP.GetRequestStream();
                // 每次读文件流的2kb
                contentLen = fs.Read(buff, 0, buffLength);
                int allbye = (int)fileInf.Length;
                int startbye = 0;
                // 流内容没有结束
                while (contentLen != 0)
                {
                    // 把内容从file stream 写入 upload stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                    startbye += contentLen;
                    if (this.progressBar.Value != this.progressBar.Maximum)
                    {
                         
                        this.progressBar.Value ++;
                    }                }
                // 关闭两个流
                strm.Close();
                fs.Close();
                this.lalSuccess.Visible = true;
                              m_upload = true;
                            }
            catch (Exception ex)
            {
                m_upload = false;
                         }
        }

解决方案 »

  1.   

    while (contentLen != 0) 
                    { 
                        // 把内容从file stream 写入 upload stream 
                        strm.Write(buff, 0, contentLen); 
                        contentLen = fs.Read(buff, 0, buffLength); 
                        startbye += contentLen; 
                        this.progressBar.Value = 100*startbye/allbye;
                    }