Nessuna descrizione

uploadFile.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
  37. int buffLength = 2048;// 缓冲大小设置为2kb
  38. byte[] buff = new byte[buffLength];
  39. int contentLen;
  40. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  41. FileStream fs = fileInf.OpenRead();
  42. try
  43. {
  44. Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
  45. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  46. while (contentLen != 0)// 流内容没有结束
  47. {
  48. // 把内容从file stream 写入 upload stream
  49. strm.Write(buff, 0, contentLen);
  50. contentLen = fs.Read(buff, 0, buffLength);
  51. }
  52. // 关闭两个流
  53. strm.Close();
  54. fs.Close();
  55. res = "上传成功!";
  56. }
  57. catch (Exception ex)
  58. {
  59. res = "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>";
  60. }
  61. return res;
  62. }
  63. /// <summary>
  64. /// 判断ftp服务器上该目录是否存在
  65. /// </summary>
  66. /// <param name="ftpPath">FTP路径目录</param>
  67. /// <param name="dirName">目录上的文件夹名称</param>
  68. /// <returns></returns>
  69. private bool CheckDirectoryExist(string ftpPath, string dirName)
  70. {
  71. bool flag = true;
  72. try
  73. {
  74. //实例化FTP连接
  75. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
  76. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  77. ftp.Method = WebRequestMethods.Ftp.ListDirectory;
  78. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  79. response.Close();
  80. }
  81. catch (Exception)
  82. {
  83. flag = false;
  84. }
  85. return flag;
  86. }
  87. /// <summary>
  88. /// 创建文件夹
  89. /// </summary>
  90. /// <param name="ftpPath">FTP路径</param>
  91. /// <param name="dirName">创建文件夹名称</param>
  92. public void MakeDir(string ftpPath, string dirName)
  93. {
  94. FtpWebRequest reqFTP;
  95. try
  96. {
  97. string ui = (ftpPath + dirName).Trim();
  98. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
  99. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  100. reqFTP.UseBinary = true;
  101. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  102. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  103. Stream ftpStream = response.GetResponseStream();
  104. ftpStream.Close();
  105. response.Close();
  106. //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
  107. }
  108. catch (Exception ex)
  109. {
  110. //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
  111. }
  112. }
  113. }
  114. }