Nav apraksta

uploadFile.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CallCenter.Utility
  9. {
  10. public class uploadFile
  11. {
  12. public string ftpPath;
  13. public string ftpUserID;
  14. public string ftpPassword;
  15. /// <summary>
  16. /// 上传文件
  17. /// </summary>
  18. /// <param name="fileinfo">需要上传的文件</param>
  19. public string UploadLocalToFtp(string localFile)
  20. {
  21. string res = "";
  22. if (!File.Exists(localFile))
  23. {
  24. res = "文件:“" + localFile + "” 不存在!";
  25. return res;
  26. }
  27. FileInfo fileInf = new FileInfo(localFile);
  28. string ftpURI = "ftp://" + ftpPath + "/";
  29. string uri = ftpURI + fileInf.Name;
  30. FtpWebRequest reqFTP;
  31. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象
  32. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  33. reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  34. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  35. reqFTP.UseBinary = true;// 指定数据传输类型
  36. reqFTP.UsePassive = true;
  37. reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
  38. int buffLength = 2048;// 缓冲大小设置为2kb
  39. byte[] buff = new byte[buffLength];
  40. int contentLen;
  41. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  42. FileStream fs = fileInf.OpenRead();
  43. try
  44. {
  45. res += uri+localFile;
  46. Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
  47. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  48. while (contentLen != 0)// 流内容没有结束
  49. {
  50. // 把内容从file stream 写入 upload stream
  51. strm.Write(buff, 0, contentLen);
  52. contentLen = fs.Read(buff, 0, buffLength);
  53. }
  54. // 关闭两个流
  55. strm.Close();
  56. fs.Close();
  57. res = "上传成功!";
  58. }
  59. catch (Exception ex)
  60. {
  61. res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:"+localFile;
  62. }
  63. return res;
  64. }
  65. /// <summary>
  66. /// 判断ftp服务器上该目录是否存在
  67. /// </summary>
  68. /// <param name="ftpPath">FTP路径目录</param>
  69. /// <param name="dirName">目录上的文件夹名称</param>
  70. /// <returns></returns>
  71. private bool CheckDirectoryExist(string ftpPath, string dirName)
  72. {
  73. bool flag = true;
  74. try
  75. {
  76. //实例化FTP连接
  77. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
  78. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  79. ftp.Method = WebRequestMethods.Ftp.ListDirectory;
  80. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  81. response.Close();
  82. }
  83. catch (Exception)
  84. {
  85. flag = false;
  86. }
  87. return flag;
  88. }
  89. /// <summary>
  90. /// 创建文件夹
  91. /// </summary>
  92. /// <param name="ftpPath">FTP路径</param>
  93. /// <param name="dirName">创建文件夹名称</param>
  94. public void MakeDir(string ftpPath, string dirName)
  95. {
  96. FtpWebRequest reqFTP;
  97. try
  98. {
  99. string ui = (ftpPath + dirName).Trim();
  100. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
  101. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  102. reqFTP.UseBinary = true;
  103. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  104. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  105. Stream ftpStream = response.GetResponseStream();
  106. ftpStream.Close();
  107. response.Close();
  108. //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
  109. }
  110. catch (Exception ex)
  111. {
  112. //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
  113. }
  114. }
  115. }
  116. }