鄂尔多斯-招源科技

FileUp.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Web;
  5. using System.Web.UI.WebControls;
  6. namespace CallCenter.Utility
  7. {
  8. /// <summary>
  9. /// 文件上传类
  10. /// </summary>
  11. public class FileUp
  12. {
  13. public FileUp()
  14. { }
  15. /// <summary>
  16. /// 转换为字节数组
  17. /// </summary>
  18. /// <param name="filename">文件名</param>
  19. /// <returns>字节数组</returns>
  20. public byte[] GetBinaryFile(string filename)
  21. {
  22. if (File.Exists(filename))
  23. {
  24. FileStream Fsm = null;
  25. try
  26. {
  27. Fsm = File.OpenRead(filename);
  28. return this.ConvertStreamToByteBuffer(Fsm);
  29. }
  30. catch
  31. {
  32. return new byte[0];
  33. }
  34. finally
  35. {
  36. Fsm.Close();
  37. }
  38. }
  39. else
  40. {
  41. return new byte[0];
  42. }
  43. }
  44. /// <summary>
  45. /// 流转化为字节数组
  46. /// </summary>
  47. /// <param name="theStream">流</param>
  48. /// <returns>字节数组</returns>
  49. public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
  50. {
  51. int bi;
  52. MemoryStream tempStream = new System.IO.MemoryStream();
  53. try
  54. {
  55. while ((bi = theStream.ReadByte()) != -1)
  56. {
  57. tempStream.WriteByte(((byte)bi));
  58. }
  59. return tempStream.ToArray();
  60. }
  61. catch
  62. {
  63. return new byte[0];
  64. }
  65. finally
  66. {
  67. tempStream.Close();
  68. }
  69. }
  70. /// <summary>
  71. /// 上传文件
  72. /// </summary>
  73. /// <param name="PosPhotoUpload">控件</param>
  74. /// <param name="saveFileName">保存的文件名</param>
  75. /// <param name="imagePath">保存的文件路径</param>
  76. public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)
  77. {
  78. string state = "";
  79. if (PosPhotoUpload.HasFile)
  80. {
  81. if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)
  82. {
  83. string MimeType = PosPhotoUpload.PostedFile.ContentType;
  84. if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))
  85. {
  86. string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);
  87. PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));
  88. }
  89. else
  90. {
  91. state = "上传文件类型不正确";
  92. }
  93. }
  94. else
  95. {
  96. state = "上传文件不能大于10M";
  97. }
  98. }
  99. else
  100. {
  101. state = "没有上传文件";
  102. }
  103. return state;
  104. }
  105. /// <summary>
  106. /// 上传文件
  107. /// </summary>
  108. /// <param name="binData">字节数组</param>
  109. /// <param name="fileName">文件名</param>
  110. /// <param name="fileType">文件类型</param>
  111. //-------------------调用----------------------
  112. //byte[] by = GetBinaryFile("E:\\Hello.txt");
  113. //this.SaveFile(by,"Hello",".txt");
  114. //---------------------------------------------
  115. public void SaveFile(byte[] binData, string SavePath, string fileName, string fileType)
  116. {
  117. FileStream fileStream = null;
  118. MemoryStream m = new MemoryStream(binData);
  119. try
  120. {
  121. string savePath = HttpContext.Current.Server.MapPath(SavePath);
  122. if (!Directory.Exists(savePath))
  123. {
  124. Directory.CreateDirectory(savePath);
  125. }
  126. string File = savePath + fileName + fileType;
  127. fileStream = new FileStream(File, FileMode.Create);
  128. m.WriteTo(fileStream);
  129. }
  130. finally
  131. {
  132. m.Close();
  133. fileStream.Close();
  134. }
  135. }
  136. /// <summary>
  137. /// FTP上传文件
  138. /// </summary>
  139. /// <param name="fileinfo">需要上传的文件</param>
  140. /// <param name="targetDir">目标路径</param>
  141. /// <param name="hostname">ftp地址</param>
  142. /// <param name="username">ftp用户名</param>
  143. /// <param name="password">ftp密码</param>
  144. public static string UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
  145. {
  146. //1. check target
  147. string target;
  148. if (targetDir.Trim() == "")
  149. {
  150. return "";
  151. }
  152. target = Guid.NewGuid().ToString(); //使用临时文件名
  153. string URI = "FTP://" + hostname + "/" + targetDir + "/" + target;
  154. ///WebClient webcl = new WebClient();
  155. System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
  156. //设置FTP命令 设置所要执行的FTP命令,
  157. //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表
  158. ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  159. //指定文件传输的数据类型
  160. ftp.UseBinary = true;
  161. ftp.UsePassive = true;
  162. //告诉ftp文件大小
  163. ftp.ContentLength = fileinfo.Length;
  164. //缓冲大小设置为2KB
  165. const int BufferSize = 204800;
  166. byte[] content = new byte[BufferSize - 1 + 1];
  167. int dataRead;
  168. //打开一个文件流 (System.IO.FileStream) 去读上传的文件
  169. using (FileStream fs = fileinfo.OpenRead())
  170. {
  171. try
  172. {
  173. //把上传的文件写入流
  174. using (Stream rs = ftp.GetRequestStream())
  175. {
  176. do
  177. {
  178. //每次读文件流的2KB
  179. dataRead = fs.Read(content, 0, BufferSize);
  180. rs.Write(content, 0, dataRead);
  181. } while (!(dataRead < BufferSize));
  182. rs.Close();
  183. }
  184. }
  185. catch (Exception ex) { }
  186. finally
  187. {
  188. fs.Close();
  189. }
  190. }
  191. ftp = null;
  192. //设置FTP命令
  193. ftp = GetRequest(URI, username, password);
  194. ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名
  195. ftp.RenameTo = fileinfo.Name;
  196. try
  197. {
  198. ftp.GetResponse();
  199. return fileinfo.Name;
  200. }
  201. catch (Exception ex)
  202. {
  203. ftp = GetRequest(URI, username, password);
  204. ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除
  205. ftp.GetResponse();
  206. throw ex;
  207. }
  208. finally
  209. {
  210. //fileinfo.Delete();
  211. }
  212. // 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
  213. ftp = null;
  214. #region
  215. /*****
  216. *FtpWebResponse
  217. * ****/
  218. //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse();
  219. #endregion
  220. }
  221. private static FtpWebRequest GetRequest(string URI, string username, string password)
  222. {
  223. //根据服务器信息FtpWebRequest创建类的对象
  224. FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
  225. //提供身份验证信息
  226. result.Credentials = new System.Net.NetworkCredential(username, password);
  227. //设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true
  228. result.KeepAlive = false;
  229. return result;
  230. }
  231. }
  232. }