説明なし

uploadFile.cs 7.2KB

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