如题。望各位大大不吝赐教。

解决方案 »

  1.   

    private static void Upload(string ftpServer, string userName, string password, string filename)
    {
       using (System.Net.WebClient client = new System.Net.WebClient())
       {
          client.Credentials = new System.Net.NetworkCredential(userName, password);
          client.UploadFile(ftpServer + "/" + new FileInfo(filename).Name, "STOR", filename);
       }
    } 调用Upload("ftp://ftpserver.com", "TheUserName", "ThePassword", @"C:\file.txt"); 通过使用FTPwebrequest上传文件
    或web services
    private bool FtpUpFile(string strFileName,string strFtpUrl,string strFtpUser,string strFtpPassword)  
      {  
      try  
      {  
      FileInfo fileInf = new FileInfo(strFileName);  
      FtpWebRequest reqFTP;  
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + fileInf.Name));  
      reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPassword);  
      reqFTP.KeepAlive = false;  
      reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
      reqFTP.UseBinary = true;  
      reqFTP.ContentLength = fileInf.Length;  
      int buffLength = 2048;  
      byte[] buff = new byte[buffLength];  
      int contentLen;  
      FileStream fs = fileInf.OpenRead();  
      Stream strm = reqFTP.GetRequestStream();  
      contentLen = fs.Read(buff, 0, buffLength);  
      int allbye = (int)fileInf.Length;  
      int startbye = 0;  
      while (contentLen != 0)  
      {  
      strm.Write(buff, 0, contentLen);  
      contentLen = fs.Read(buff, 0, buffLength);  
      startbye += contentLen;  
      }  
      strm.Close();  
      fs.Close();  
      return true;  
      }  
      catch  
      {  
      return false;  
      }  
      }
    还可使用webclient
    System.Net.WebClient myWebClient = new System.Net.WebClient();
    myWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727;)");
    myWebClient.UploadFile(webUrl, "POST", localFileName);
    /// <summary>
      /// FTP上传文件方法,设定ftp下的保存路径,文件名
      /// </summary>
      /// <param name="ftpServerIP">ftp地址</param>
      /// <param name="ftpUserID">ftp用户名</param>
      /// <param name="ftpPassword">ftp密码</param>
      /// <param name="streamRead">上传文件流</param>
      /// <param name="savePath">文件名(可带相对路径)</param>
      /// <exception >throw</exception>
      protected void UploadFileByFTP(string ftpServerIP, string ftpUserID, string ftpPassword, Stream streamRead, string filename)
      {
      string l_notice = "";//上传提示
      string uri = string.Empty;
      //如果filename为空提示选择文件 
      if (filename == null || filename == "")
      {
      l_notice = "Please select File !";
      }
      else
      {
      try
      {   
      if (filename.StartsWith("/"))
      {
      filename = filename.Remove(0, 1);
      }
      uri = "ftp://" + ftpServerIP + "/" + filename;  FtpWebRequest reqFTP;
      // 根据uri创建FtpWebRequest对象 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  // ftp用户名和密码 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  // 默认为true,连接不会被关闭 
      // 在一个命令之后被执行 
      reqFTP.KeepAlive = false;  // 指定执行命令 
      reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  // 指定数据传输类型 
      reqFTP.UseBinary = true;  // 上传文件时通知服务器文件的大小 
      reqFTP.ContentLength = streamRead.Length;  // 缓冲大小设置为5kb 
      int buffLength = 5048;  byte[] buff = new byte[buffLength];
      int contentLen;
      Stream streamWrite = reqFTP.GetRequestStream();
      streamRead.Flush();
      contentLen = streamRead.Read(buff, 0, buffLength);
      while (contentLen != 0)
      {
      streamWrite.Write(buff, 0, contentLen);
      contentLen = streamRead.Read(buff, 0, buffLength);
      }
      // 关闭两个流 
      streamWrite.Close();
      }
      catch (Exception ex)
      {
      throw new Exception("此处是上传方法:"+ex.Message + "ftp路径:" + uri);
      }
      }
      Response.Write(l_notice);
      }
    ///  <summary> 
         /// 建立DOS连接 
         ///  </summary> 
         ///  <param name="path">要建立连接的服务器IP以及路径 </param> 
         static public void DOSConnected() 
         { 
             string path = GetServerSavePath(); 
             Process proc = new Process(); //实例化一个Process类 
             proc.StartInfo.FileName = "cmd.exe";  //设定程序名 
             proc.StartInfo.Arguments = "/c net use Z: \\\\" + path;  //设定程式执行参数,此命令意思是将参数path代表的服务器路径虚拟为本地的Z盘 
             proc.StartInfo.CreateNoWindow = false;  //设置不显示窗口 
             proc.StartInfo.UseShellExecute = false; //关闭Shell的使用 
             proc.StartInfo.RedirectStandardInput = true;//重定向标准输入 
             proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出 
             proc.StartInfo.RedirectStandardError = true; //重定向错误输出 
             proc.Start();                                //启动  
             proc.WaitForExit(); 
         } 
         ///  <summary> 
         /// 文件拷贝 从网站服务器拷贝到文件服务器 
         ///  </summary> 
         ///  <param name="FilePath">本地文件路径 </param> 
         static public void DOSXCopy(string FilePath) 
         { 
             try 
             { 
                 Process proc = new Process(); //实例化一个Process类 
                 proc.StartInfo.FileName = "cmd.exe";  //设定程序名 
                 proc.StartInfo.Arguments = "/c XCOPY " + FilePath + " Z:\\";//此命令意思是使用XCOPY命令将FilePath表示的文件拷贝到Z盘 
                 proc.StartInfo.CreateNoWindow = false;  //设置不显示窗口 
                 proc.StartInfo.UseShellExecute = false; //关闭Shell的使用 
                 proc.StartInfo.RedirectStandardInput = true;//重定向标准输入 
                 proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出 
                 proc.StartInfo.RedirectStandardError = true; //重定向错误输出 
                 proc.Start(); 
                 proc.WaitForExit(); 
             } 
             catch(Exception ex) 
             { 
                 string time = System.DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分", DateTimeFormatInfo.InvariantInfo); 
                 string sss = "图片保存日志.txt"; 
                 //指定路径 
                 sss = "d:\\" + sss; 
                 //如果文件a.txt存在就打开,不存在就新建 .append 是追加写 
                 FileStream fst = new FileStream(sss, FileMode.Append); 
                 //写数据到a.txt格式 
                 StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8")); 
                 //写入 
                 swt.WriteLine(time + '\r' + "保存路径为:" + '\r' + FilePath + '\r' + "出错为:" + '\r' + ex); 
                 swt.Close(); 
                 fst.Close(); 
             } 
         }
    那是各种传啊 随便找个吧
      

  2.   

    ,fuck 没弄懂,  这个 FTP