Няма описание

uploadFile.cs 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. //改后文件
  29. #region 改后文件
  30. //string temp_filename = localFile.Split('\\').Last().Split('.')[0] + "_temp";
  31. //string localfileAft = Path.GetDirectoryName(localFile) + "\\" + temp_filename + ".wav";
  32. //FileInfo fileInfAft = new FileInfo(localfileAft);
  33. #endregion
  34. string ftpURI = "ftp://" + ftpPath + "/";
  35. string uri = ftpURI + fileInf.Name;
  36. //string uriAft = ftpURI + fileInfAft.Name;
  37. FtpWebRequest reqFTP;
  38. //20180209
  39. //System.GC.Collect();
  40. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象
  41. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  42. reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  43. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  44. reqFTP.UseBinary = true;// 指定数据传输类型
  45. reqFTP.UsePassive = true;
  46. reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
  47. int buffLength = 2048;// 缓冲大小设置为2kb
  48. byte[] buff = new byte[buffLength];
  49. int contentLen;
  50. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  51. //FileStream fs = fileInf.OpenRead();
  52. using (FileStream fs = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  53. {
  54. try
  55. {
  56. res += uri + localFile;
  57. using (Stream strm = reqFTP.GetRequestStream())// 把上传的文件写入流
  58. {
  59. //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
  60. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  61. while (contentLen != 0)// 流内容没有结束
  62. {
  63. // 把内容从file stream 写入 upload stream
  64. strm.Write(buff, 0, contentLen);
  65. contentLen = fs.Read(buff, 0, buffLength);
  66. }
  67. //StringBuilder sb = new StringBuilder();
  68. //while (!strm.EndOfStream)
  69. //{
  70. // sb.AppendLine(strm.ReadLine() + "<br>");
  71. //}
  72. }
  73. // 关闭两个流
  74. //strm.Close();
  75. fs.Close();
  76. res = "上传成功!";
  77. }
  78. catch (Exception ex)
  79. {
  80. res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
  81. }
  82. }
  83. //FtpWebRequest reqFTP1;
  84. ////20180209
  85. ////System.GC.Collect();
  86. //reqFTP1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(uriAft));// 根据uri创建FtpWebRequest对象
  87. //reqFTP1.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  88. //reqFTP1.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  89. //reqFTP1.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  90. //reqFTP1.UseBinary = true;// 指定数据传输类型
  91. //reqFTP1.UsePassive = true;
  92. //reqFTP1.ContentLength = fileInfAft.Length;// 上传文件时通知服务器文件的大小
  93. //int buffLengthaft = 2048;// 缓冲大小设置为2kb
  94. //byte[] buffaft = new byte[buffLengthaft];
  95. //int contentLenaft;
  96. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  97. //FileStream fs = fileInf.OpenRead();
  98. //using (FileStream fs1 = new FileStream(localfileAft, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  99. //{
  100. // try
  101. // {
  102. // res += uriAft + localfileAft;
  103. // using (Stream strm1 = reqFTP1.GetRequestStream())// 把上传的文件写入流
  104. // {
  105. // //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
  106. // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);// 每次读文件流的2kb
  107. // while (contentLenaft != 0)// 流内容没有结束
  108. // {
  109. // // 把内容从file stream 写入 upload stream
  110. // strm1.Write(buffaft, 0, contentLenaft);
  111. // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);
  112. // }
  113. // //StringBuilder sb = new StringBuilder();
  114. // //while (!strm.EndOfStream)
  115. // //{
  116. // // sb.AppendLine(strm.ReadLine() + "<br>");
  117. // //}
  118. // }
  119. // // 关闭两个流
  120. // //strm.Close();
  121. // fs1.Close();
  122. // res = "上传成功!";
  123. // }
  124. // catch (Exception ex)
  125. // {
  126. // res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
  127. // }
  128. //}
  129. return res;
  130. }
  131. /// <summary>
  132. /// 判断ftp服务器上该目录是否存在
  133. /// </summary>
  134. /// <param name="ftpPath">FTP路径目录</param>
  135. /// <param name="dirName">目录上的文件夹名称</param>
  136. /// <returns></returns>
  137. private bool CheckDirectoryExist(string ftpPath, string dirName)
  138. {
  139. bool flag = true;
  140. try
  141. {
  142. //实例化FTP连接
  143. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
  144. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  145. ftp.Method = WebRequestMethods.Ftp.ListDirectory;
  146. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  147. response.Close();
  148. }
  149. catch (Exception)
  150. {
  151. flag = false;
  152. }
  153. return flag;
  154. }
  155. /// <summary>
  156. /// 创建文件夹
  157. /// </summary>
  158. /// <param name="ftpPath">FTP路径</param>
  159. /// <param name="dirName">创建文件夹名称</param>
  160. public void MakeDir(string ftpPath, string dirName)
  161. {
  162. FtpWebRequest reqFTP;
  163. try
  164. {
  165. string ui = (ftpPath + dirName).Trim();
  166. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
  167. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  168. reqFTP.UseBinary = true;
  169. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  170. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  171. Stream ftpStream = response.GetResponseStream();
  172. ftpStream.Close();
  173. response.Close();
  174. //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
  175. }
  176. catch (Exception ex)
  177. {
  178. //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
  179. }
  180. }
  181. }
  182. }