Sin descripción

uploadFile.cs 7.3KB

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