遍历listbox中的文件名,每取一个上传一次,直到上传结束

解决方案 »

  1.   

    public void UpLoad(string fileName)
            {
                FileInfo fileInfo = new FileInfo(fileName);            string uri = "ftp://" + ftpServerIP + "/" + fileInfo.Name;            FtpWebRequest reqFTP;            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//用户名和密码            reqFTP.KeepAlive = false;            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令            reqFTP.UseBinary = true; // 指定数据传输类型            reqFTP.ContentLength = fileInfo.Length;// 上传文件时通知服务器文件的大小            int buffLength = 2048;// 缓冲大小设置为2kb            byte[] buff = new byte[buffLength];            int contentLen;            FileStream fs = fileInfo.OpenRead();
                try
                {
                    Stream strm = reqFTP.GetRequestStream();                contentLen = fs.Read(buff, 0, buffLength);                while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);                    contentLen = fs.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fs.Close();            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Upload   Error ");
                }
                
            }        private void but_UpLoad_Click(object sender, EventArgs e)
            {
                if (this.txt_FTPAddress.Text != "" && this.listBox_FileName.Text != "")
                {
                    ftpServerIP = this.txt_FTPAddress.Text;
                    ftpUserID = this.txt_User.Text;
                    ftpPassword = this.txt_Pwd.Text;
                    this.UpLoad(this.listBox_FileName.Text);
                    MessageBox.Show("文件上传成功!");
                }
                else
                {
                    MessageBox.Show("FTP地址和文件名不能为空!请填写地址或者选择文件名!");
                }
            }这样的话 我的UpLoad方法是不是要改下呢?
      

  2.   

    改成下面的private void but_UpLoad_Click(object sender, EventArgs e)
      {
      if (this.txt_FTPAddress.Text != "" && this.listBox_FileName.Text != "")
      {
      ftpServerIP = this.txt_FTPAddress.Text;
      ftpUserID = this.txt_User.Text;
      ftpPassword = this.txt_Pwd.Text;
    foreach (string itemValue in listBox1.Items)
                {
                    this.UpLoad(itemValue);
                } 
      
      MessageBox.Show("文件上传成功!");
      }
      else
      {
      MessageBox.Show("FTP地址和文件名不能为空!请填写地址或者选择文件名!");
      }
      }
      

  3.   

    看这里
    http://topic.csdn.net/u/20100427/11/a8e71a24-fa93-41db-8a2d-14cc59d59400.html
      

  4.   

    deknight
      我想问你下 我这样上传的时候有个问题 就是 我上次文件的时候文件名带有#号的话 上传成功后他就把#号和#号后的所有文件名删除! 这个是什么原因呢?
      

  5.   

    带#号被删应该是FTP端的操作吧,具体不清楚,可能跟它的命令有冲突?
      

  6.   

    deknight :
      现在还有个问题就是 我想上传到指定的FTP文件路径 应该怎么处理呢?
      

  7.   

    遍历listbox使用ftpwebrequest 等实现文件上传
    foreach (object obj in this.listBox1.Items)
    {
        
    }