暫無描述

uploadFile.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. FileStream fs = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  44. try
  45. {
  46. res += uri+localFile;
  47. //Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
  48. StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
  49. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  50. //while (contentLen != 0)// 流内容没有结束
  51. //{
  52. // // 把内容从file stream 写入 upload stream
  53. // strm.Write(buff, 0, contentLen);
  54. // contentLen = fs.Read(buff, 0, buffLength);
  55. //}
  56. StringBuilder sb = new StringBuilder();
  57. while (!strm.EndOfStream)
  58. {
  59. sb.AppendLine(strm.ReadLine() + "<br>");
  60. }
  61. // 关闭两个流
  62. strm.Close();
  63. fs.Close();
  64. res = "上传成功!";
  65. }
  66. catch (Exception ex)
  67. {
  68. res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:"+localFile;
  69. }
  70. return res;
  71. }
  72. /// <summary>
  73. /// 判断ftp服务器上该目录是否存在
  74. /// </summary>
  75. /// <param name="ftpPath">FTP路径目录</param>
  76. /// <param name="dirName">目录上的文件夹名称</param>
  77. /// <returns></returns>
  78. private bool CheckDirectoryExist(string ftpPath, string dirName)
  79. {
  80. bool flag = true;
  81. try
  82. {
  83. //实例化FTP连接
  84. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
  85. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  86. ftp.Method = WebRequestMethods.Ftp.ListDirectory;
  87. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  88. response.Close();
  89. }
  90. catch (Exception)
  91. {
  92. flag = false;
  93. }
  94. return flag;
  95. }
  96. /// <summary>
  97. /// 创建文件夹
  98. /// </summary>
  99. /// <param name="ftpPath">FTP路径</param>
  100. /// <param name="dirName">创建文件夹名称</param>
  101. public void MakeDir(string ftpPath, string dirName)
  102. {
  103. FtpWebRequest reqFTP;
  104. try
  105. {
  106. string ui = (ftpPath + dirName).Trim();
  107. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
  108. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  109. reqFTP.UseBinary = true;
  110. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  111. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  112. Stream ftpStream = response.GetResponseStream();
  113. ftpStream.Close();
  114. response.Close();
  115. //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
  116. }
  117. catch (Exception ex)
  118. {
  119. //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
  120. }
  121. }
  122. }
  123. }