Winform 上传文件(ftp方式)

  1. 就我知道而言 winform上传文件有两种方式  
  2.   
  3. 1种用FTP。这个方式比较推荐的方式。只是复杂一点。需要架设FTP服务器。还必须FTP好多API要了解。  
  4.   
  5. 但是功能比较强大。在客户端能操作服务器上任何文件,可以新建文件夹。  
  6.   
  7. 2用WebClient控件 服务器用ASP.NET或者JSP方式。其实就是我们常用的HTML浏览本地文件然后上传的功能。只不过客户端用一个控件代替了。但是服务器端还必须要ASP.NET页面来接收这个文件。这个方式客户端不能实现文件管理。文件管理都必须在服务器的ASP.NET页面里面来做管理。  
  8.   
  9. 3还一个方式是HttpWebRequest.但是这个方式的原理还是WebClient。还是直接推荐用WebClient。不推荐用HttpWebRequest  
  10.   
  11. 1:FTP方式。通过在IIS里架设FTP服务器。代码  
  12.   
  13. using System;  
  14. using System.Collections.Generic;  
  15. using System.Text;  
  16. using System.Net;  
  17. using System.IO;  
  18. using System.Windows.Forms;  
  19.   
  20. namespace net.emice.TEST1  
  21. {  
  22.     class FtpUpDown  
  23.     {  
  24.    string ftpServerIP;  
  25.   
  26.    public string FtpServerIP  
  27.    {  
  28.     get { return ftpServerIP; }  
  29.     set { ftpServerIP = value; }  
  30.    }  
  31.    string ftpUserID;  
  32.   
  33.    public string FtpUserID  
  34.    {  
  35.     get { return ftpUserID; }  
  36.     set { ftpUserID = value; }  
  37.    }  
  38.    string ftpPassword;  
  39.   
  40.    public string FtpPassword  
  41.    {  
  42.     get { return ftpPassword; }  
  43.     set { ftpPassword = value; }  
  44.    }  
  45.         FtpWebRequest reqFTP;  
  46.   
  47.         private void Connect(String path)//连接ftp  
  48.         {  
  49.             // 根据uri创建FtpWebRequest对象  
  50.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));  
  51.             // 指定数据传输类型  
  52.             reqFTP.UseBinary = true;  
  53.             // ftp用户名和密码  
  54.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  55.         }  
  56.         public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)  
  57.         {  
  58.             this.ftpServerIP = ftpServerIP;  
  59.             this.ftpUserID = ftpUserID;  
  60.             this.ftpPassword = ftpPassword;  
  61.         }  
  62.         //都调用这个  
  63.         private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表  
  64.         {  
  65.             string[] downloadFiles;  
  66.             StringBuilder result = new StringBuilder();  
  67.             try  
  68.             {  
  69.                 Connect(path);  
  70.                 reqFTP.Method = WRMethods;  
  71.                 WebResponse response = reqFTP.GetResponse();  
  72.                 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名  
  73.                 string line = reader.ReadLine();  
  74.                 while (line != null)  
  75.                 {  
  76.                     result.Append(line);  
  77.                     result.Append("/n");  
  78.                     line = reader.ReadLine();  
  79.                 }  
  80.                 // to remove the trailing '/n'  
  81.                 result.Remove(result.ToString().LastIndexOf('/n'), 1);  
  82.                 reader.Close();  
  83.                 response.Close();  
  84.                 return result.ToString().Split('/n');  
  85.             }  
  86.             catch (Exception ex)  
  87.             {  
  88.                 System.Windows.Forms.MessageBox.Show(ex.Message);  
  89.                 downloadFiles = null;  
  90.                 return downloadFiles;  
  91.             }  
  92.         }  
  93.         public string[] GetFileList(string path)//上面的代码示例了如何从ftp服务器上获得文件列表  
  94.         {  
  95.             return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectory);  
  96.         }  
  97.         public string[] GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表  
  98.         {  
  99.             return GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectory);  
  100.         }  
  101.   
  102.         public string Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能  
  103.         {  
  104.             FileInfo fileInf = new FileInfo(filename);  
  105.             string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  106.             Connect(uri);//连接           
  107.             // 默认为true,连接不会被关闭  
  108.             // 在一个命令之后被执行  
  109.             reqFTP.KeepAlive = false;  
  110.             // 指定执行什么命令  
  111.             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  112.             // 上传文件时通知服务器文件的大小  
  113.             reqFTP.ContentLength = fileInf.Length;  
  114.             // 缓冲大小设置为kb  
  115.             int buffLength = 2048;  
  116.             byte[] buff = new byte[buffLength];  
  117.             int contentLen;  
  118.             // 打开一个文件流(System.IO.FileStream) 去读上传的文件  
  119.             FileStream fs = fileInf.OpenRead();  
  120.             try  
  121.             {  
  122.                 // 把上传的文件写入流  
  123.                 Stream strm = reqFTP.GetRequestStream();  
  124.                 // 每次读文件流的kb  
  125.                 contentLen = fs.Read(buff, 0, buffLength);  
  126.                 // 流内容没有结束  
  127.                 while (contentLen != 0)  
  128.                 {  
  129.                    // 把内容从file stream 写入upload stream  
  130.                     strm.Write(buff, 0, contentLen);  
  131.                     contentLen = fs.Read(buff, 0, buffLength);  
  132.                 }  
  133.                 // 关闭两个流  
  134.                 strm.Close();  
  135.                 fs.Close();  
  136.      return "";  
  137.             }  
  138.            catch (Exception ex)  
  139.            {  
  140.                 return "Upload Error"+ex.Message;  
  141.             }  
  142.         }  
  143.         public string Download(string filePath, string fileName)上面的代码实现了从ftp服务器下载文件的功能  
  144.         {  
  145.             try  
  146.             {  
  147.                 String onlyFileName = Path.GetFileName(fileName);  
  148.                 string newFileName = filePath + "//" + onlyFileName;  
  149.                 if (File.Exists(newFileName))  
  150.                 {  
  151.       return "本地文件" + newFileName + "已存在,无法下载";  
  152.                 }  
  153.                 string url = "ftp://" + ftpServerIP + "/" + fileName;  
  154.                 Connect(url);//连接   
  155.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  156.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  157.                 Stream ftpStream = response.GetResponseStream();  
  158.                 long cl = response.ContentLength;  
  159.                 int bufferSize = 2048;  
  160.                 int readCount;  
  161.                 byte[] buffer = new byte[bufferSize];  
  162.                 readCount = ftpStream.Read(buffer, 0, bufferSize);  
  163.                 FileStream outputStream = new FileStream(newFileName, FileMode.Create);  
  164.                 while (readCount > 0)  
  165.                 {  
  166.                     outputStream.Write(buffer, 0, readCount);  
  167.                     readCount = ftpStream.Read(buffer, 0, bufferSize);  
  168.                 }  
  169.                 ftpStream.Close();  
  170.                 outputStream.Close();  
  171.                 response.Close();  
  172.   
  173.                 return "";  
  174.             }  
  175.            catch (Exception ex)  
  176.             {   
  177.                 return "因"+ex.Message+",无法下载";  
  178.             }  
  179.         }  
  180.         //删除文件  
  181.         public void DeleteFileName(string fileName)  
  182.         {  
  183.            try  
  184.             {  
  185.                 FileInfo fileInf = new FileInfo(fileName);  
  186.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  187.                 Connect(uri);//连接           
  188.                 // 默认为true,连接不会被关闭  
  189.                 // 在一个命令之后被执行  
  190.                 reqFTP.KeepAlive = false;  
  191.                 // 指定执行什么命令  
  192.                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;  
  193.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  194.                response.Close();  
  195.             }  
  196.             catch (Exception ex)  
  197.             {  
  198.                 MessageBox.Show(ex.Message, "删除错误");  
  199.             }  
  200.         }  
  201.         //创建目录  
  202.         public string MakeDir(string dirName)  
  203.         {  
  204.            try  
  205.             {  
  206.                string uri = "ftp://" + ftpServerIP + "/" + dirName;  
  207.                 Connect(uri);//连接        
  208.                 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;  
  209.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  210.                 response.Close();  
  211.      return "";  
  212.             }  
  213.             catch (WebException ex)  
  214.             {  
  215.                 return "[Make Dir]"+ex.Message;  
  216.             }  
  217.         }  
  218.         //删除目录  
  219.         public void delDir(string dirName)  
  220.         {  
  221.            try  
  222.             {  
  223.                string uri = "ftp://" + ftpServerIP + "/" + dirName;  
  224.                 Connect(uri);//连接        
  225.                 reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;  
  226.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  227.                 response.Close();  
  228.             }  
  229.             catch (Exception ex)  
  230.             {  
  231.                 MessageBox.Show(ex.Message);  
  232.             }  
  233.         }  
  234.         //获得文件大小  
  235.         public long GetFileSize(string filename)  
  236.         {  
  237.             long fileSize = 0;  
  238.             try  
  239.             {  
  240.                 FileInfo fileInf = new FileInfo(filename);  
  241.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  242.                 Connect(uri);//连接        
  243.                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;  
  244.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  245.                 fileSize = response.ContentLength;  
  246.                 response.Close();  
  247.             }  
  248.             catch (Exception ex)  
  249.             {  
  250.                 MessageBox.Show(ex.Message);  
  251.             }  
  252.             return fileSize;  
  253.         }  
  254.         //文件改名  
  255.         public void Rename(string currentFilename, string newFilename)  
  256.         {  
  257.             try  
  258.             {  
  259.                 FileInfo fileInf = new FileInfo(currentFilename);  
  260.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  261.                 Connect(uri);//连接  
  262.                 reqFTP.Method = WebRequestMethods.Ftp.Rename;  
  263.                 reqFTP.RenameTo = newFilename;  
  264.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  265.                 //Stream ftpStream = response.GetResponseStream();  
  266.                 //ftpStream.Close();  
  267.                 response.Close();  
  268.             }  
  269.             catch (Exception ex)  
  270.             {  
  271.                 MessageBox.Show(ex.Message);  
  272.             }  
  273.         }  
  274.         //获得文件明晰  
  275.         public string[] GetFilesDetailList()  
  276.         {  
  277.             return GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectoryDetails);  
  278.         }  
  279.         //获得文件明晰  
  280.         public string[] GetFilesDetailList(string path)  
  281.        {  
  282.             return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);  
  283.         }  
  284.     }  
  285. }  
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值