商丘12345 后端

FileUp.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Web.UI.WebControls;
  5. namespace CallCenter.Utility
  6. {
  7. /// <summary>
  8. /// 文件上传类
  9. /// </summary>
  10. public class FileUp
  11. {
  12. public FileUp()
  13. { }
  14. /// <summary>
  15. /// 转换为字节数组
  16. /// </summary>
  17. /// <param name="filename">文件名</param>
  18. /// <returns>字节数组</returns>
  19. public byte[] GetBinaryFile(string filename)
  20. {
  21. if (File.Exists(filename))
  22. {
  23. FileStream Fsm = null;
  24. try
  25. {
  26. Fsm = File.OpenRead(filename);
  27. return this.ConvertStreamToByteBuffer(Fsm);
  28. }
  29. catch
  30. {
  31. return new byte[0];
  32. }
  33. finally
  34. {
  35. Fsm.Close();
  36. }
  37. }
  38. else
  39. {
  40. return new byte[0];
  41. }
  42. }
  43. /// <summary>
  44. /// 流转化为字节数组
  45. /// </summary>
  46. /// <param name="theStream">流</param>
  47. /// <returns>字节数组</returns>
  48. public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
  49. {
  50. int bi;
  51. MemoryStream tempStream = new System.IO.MemoryStream();
  52. try
  53. {
  54. while ((bi = theStream.ReadByte()) != -1)
  55. {
  56. tempStream.WriteByte(((byte)bi));
  57. }
  58. return tempStream.ToArray();
  59. }
  60. catch
  61. {
  62. return new byte[0];
  63. }
  64. finally
  65. {
  66. tempStream.Close();
  67. }
  68. }
  69. /// <summary>
  70. /// 上传文件
  71. /// </summary>
  72. /// <param name="PosPhotoUpload">控件</param>
  73. /// <param name="saveFileName">保存的文件名</param>
  74. /// <param name="imagePath">保存的文件路径</param>
  75. public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)
  76. {
  77. string state = "";
  78. if (PosPhotoUpload.HasFile)
  79. {
  80. if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)
  81. {
  82. string MimeType = PosPhotoUpload.PostedFile.ContentType;
  83. if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))
  84. {
  85. string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);
  86. PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));
  87. }
  88. else
  89. {
  90. state = "上传文件类型不正确";
  91. }
  92. }
  93. else
  94. {
  95. state = "上传文件不能大于10M";
  96. }
  97. }
  98. else
  99. {
  100. state = "没有上传文件";
  101. }
  102. return state;
  103. }
  104. /// <summary>
  105. /// 上传文件
  106. /// </summary>
  107. /// <param name="binData">字节数组</param>
  108. /// <param name="fileName">文件名</param>
  109. /// <param name="fileType">文件类型</param>
  110. //-------------------调用----------------------
  111. //byte[] by = GetBinaryFile("E:\\Hello.txt");
  112. //this.SaveFile(by,"Hello",".txt");
  113. //---------------------------------------------
  114. public void SaveFile(byte[] binData, string SavePath, string fileName, string fileType)
  115. {
  116. FileStream fileStream = null;
  117. MemoryStream m = new MemoryStream(binData);
  118. try
  119. {
  120. string savePath = HttpContext.Current.Server.MapPath(SavePath);
  121. if (!Directory.Exists(savePath))
  122. {
  123. Directory.CreateDirectory(savePath);
  124. }
  125. string File = savePath + fileName + fileType;
  126. fileStream = new FileStream(File, FileMode.Create);
  127. m.WriteTo(fileStream);
  128. }
  129. finally
  130. {
  131. m.Close();
  132. fileStream.Close();
  133. }
  134. }
  135. /// <summary>
  136. /// 上传文件
  137. /// </summary>
  138. /// <param name="hpFile">文件</param>
  139. /// <param name="path">路径</param>
  140. /// <param name="usercode">上传人</param>
  141. /// <returns></returns>
  142. public string Upload(HttpPostedFile hpFile,string path)
  143. {
  144. path = HttpContext.Current.Server.MapPath("..") + path;
  145. string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
  146. int iLen = hpFile.ContentLength;
  147. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  148. byte[] bData = new byte[iLen];
  149. hpFile.InputStream.Read(bData, 0, iLen);
  150. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  151. newFile.Write(bData, 0, bData.Length);
  152. newFile.Flush();
  153. return newName;
  154. }
  155. /// <summary>
  156. /// 上传文件
  157. /// </summary>
  158. /// <param name="hpFile">文件</param>
  159. /// <param name="path">路径</param>
  160. /// <param name="usercode">上传人</param>
  161. /// <returns></returns>
  162. public string Upload(HttpPostedFile hpFile, string path, string names)
  163. {
  164. path = HttpContext.Current.Server.MapPath("..") + "/" + path;
  165. //string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
  166. string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("."));
  167. string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType;
  168. int iLen = hpFile.ContentLength;
  169. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  170. byte[] bData = new byte[iLen];
  171. hpFile.InputStream.Read(bData, 0, iLen);
  172. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  173. newFile.Write(bData, 0, bData.Length);
  174. newFile.Flush();
  175. return newName;
  176. }
  177. /// <summary>
  178. /// 根据地址获取文件扩展名
  179. /// </summary>
  180. /// <param name="path"></param>
  181. /// <returns></returns>
  182. public string GetExt(string path)
  183. {
  184. return Path.GetExtension(path);
  185. }
  186. /// <summary>
  187. /// 根据路径获取文件名
  188. /// </summary>
  189. /// <param name="path"></param>
  190. /// <returns></returns>
  191. public string GetFileName(string path)
  192. {
  193. var filename = Path.GetFileName(path);
  194. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + filename;
  195. }
  196. }
  197. }