当前位置:编程学习 > C#/ASP.NET >>

上传文件问题?

 我用OpenFileDialog选择了多个文件到listbox里 怎么样按一个按钮就把这些文件全部上传到指定的FTP文件夹中呢? --------------------编程问答-------------------- 遍历listbox中的文件名,每取一个上传一次,直到上传结束
--------------------编程问答-------------------- 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方法是不是要改下呢? --------------------编程问答-------------------- 改成下面的

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地址和文件名不能为空!请填写地址或者选择文件名!");
  }
  }
--------------------编程问答-------------------- 看这里
http://topic.csdn.net/u/20100427/11/a8e71a24-fa93-41db-8a2d-14cc59d59400.html --------------------编程问答-------------------- deknight
  我想问你下 我这样上传的时候有个问题 就是 我上次文件的时候文件名带有#号的话 上传成功后他就把#号和#号后的所有文件名删除! 这个是什么原因呢? --------------------编程问答-------------------- 带#号被删应该是FTP端的操作吧,具体不清楚,可能跟它的命令有冲突? --------------------编程问答-------------------- deknight :
  现在还有个问题就是 我想上传到指定的FTP文件路径 应该怎么处理呢? --------------------编程问答-------------------- 留个记号........ --------------------编程问答-------------------- 思路是怎么的呢 --------------------编程问答-------------------- 遍历listbox使用ftpwebrequest 等实现文件上传
foreach (object obj in this.listBox1.Items)
{
    
}

--------------------编程问答--------------------
引用 10 楼 wuyq11 的回复:
遍历listbox使用ftpwebrequest 等实现文件上传
foreach (object obj in this.listBox1.Items)
{
   
}

顶!
同感!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,