商丘12345 后端

FileUp.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Web.UI.WebControls;
  5. namespace CallCenter_SQ.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. public void SaveFile(byte[] binData, string SavePath, string fileName)
  70. {
  71. FileStream fileStream = null;
  72. MemoryStream m = new MemoryStream(binData);
  73. try
  74. {
  75. string savePath = HttpContext.Current.Server.MapPath(SavePath);
  76. if (!Directory.Exists(savePath))
  77. {
  78. Directory.CreateDirectory(savePath);
  79. }
  80. string File = savePath + fileName;
  81. fileStream = new FileStream(File, FileMode.Create);
  82. m.WriteTo(fileStream);
  83. }
  84. finally
  85. {
  86. m.Close();
  87. fileStream.Close();
  88. }
  89. }
  90. public void ByteSaveFile(string base64String, string SavePath, string fileName)
  91. {
  92. string savePath = HttpContext.Current.Server.MapPath(SavePath);
  93. if (!Directory.Exists(savePath))
  94. {
  95. Directory.CreateDirectory(savePath);
  96. }
  97. savePath += fileName;
  98. try
  99. {
  100. using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64String)))
  101. {
  102. using (FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write))
  103. {
  104. byte[] b = stream.ToArray();
  105. fs.Write(b, 0, b.Length);
  106. }
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. throw ex;
  112. }
  113. }
  114. /// <summary>
  115. /// 上传文件
  116. /// </summary>
  117. /// <param name="PosPhotoUpload">控件</param>
  118. /// <param name="saveFileName">保存的文件名</param>
  119. /// <param name="imagePath">保存的文件路径</param>
  120. public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)
  121. {
  122. string state = "";
  123. if (PosPhotoUpload.HasFile)
  124. {
  125. if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)
  126. {
  127. string MimeType = PosPhotoUpload.PostedFile.ContentType;
  128. if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))
  129. {
  130. string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);
  131. PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));
  132. }
  133. else
  134. {
  135. state = "上传文件类型不正确";
  136. }
  137. }
  138. else
  139. {
  140. state = "上传文件不能大于10M";
  141. }
  142. }
  143. else
  144. {
  145. state = "没有上传文件";
  146. }
  147. return state;
  148. }
  149. /// <summary>
  150. /// 上传文件
  151. /// </summary>
  152. /// <param name="binData">字节数组</param>
  153. /// <param name="fileName">文件名</param>
  154. /// <param name="fileType">文件类型</param>
  155. //-------------------调用----------------------
  156. //byte[] by = GetBinaryFile("E:\\Hello.txt");
  157. //this.SaveFile(by,"Hello",".txt");
  158. //---------------------------------------------
  159. public void SaveFile(byte[] binData, string SavePath, string fileName, string fileType)
  160. {
  161. FileStream fileStream = null;
  162. MemoryStream m = new MemoryStream(binData);
  163. try
  164. {
  165. string savePath = HttpContext.Current.Server.MapPath(SavePath);
  166. if (!Directory.Exists(savePath))
  167. {
  168. Directory.CreateDirectory(savePath);
  169. }
  170. string File = savePath + fileName + fileType;
  171. fileStream = new FileStream(File, FileMode.Create);
  172. m.WriteTo(fileStream);
  173. }
  174. finally
  175. {
  176. m.Close();
  177. fileStream.Close();
  178. }
  179. }
  180. /// <summary>
  181. /// 上传文件
  182. /// </summary>
  183. /// <param name="hpFile">文件</param>
  184. /// <param name="path">路径</param>
  185. /// <param name="usercode">上传人</param>
  186. /// <returns></returns>
  187. public string Upload(HttpPostedFile hpFile,string path)
  188. {
  189. path = HttpContext.Current.Server.MapPath("..") + path;
  190. string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
  191. int iLen = hpFile.ContentLength;
  192. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  193. byte[] bData = new byte[iLen];
  194. hpFile.InputStream.Read(bData, 0, iLen);
  195. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  196. newFile.Write(bData, 0, bData.Length);
  197. newFile.Flush();
  198. return newName;
  199. }
  200. /// <summary>
  201. /// 上传文件
  202. /// </summary>
  203. /// <param name="hpFile">文件</param>
  204. /// <param name="path">路径</param>
  205. /// <param name="usercode">上传人</param>
  206. /// <returns></returns>
  207. public string Upload(HttpPostedFile hpFile, string path, string names)
  208. {
  209. path = HttpContext.Current.Server.MapPath("..") + "/" + path;
  210. //string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
  211. string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("."));
  212. string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType;
  213. int iLen = hpFile.ContentLength;
  214. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  215. byte[] bData = new byte[iLen];
  216. hpFile.InputStream.Read(bData, 0, iLen);
  217. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  218. newFile.Write(bData, 0, bData.Length);
  219. newFile.Flush();
  220. return newName;
  221. }
  222. /// <summary>
  223. /// 根据地址获取文件扩展名
  224. /// </summary>
  225. /// <param name="path"></param>
  226. /// <returns></returns>
  227. public string GetExt(string path)
  228. {
  229. return Path.GetExtension(path);
  230. }
  231. /// <summary>
  232. /// 根据路径获取文件名
  233. /// </summary>
  234. /// <param name="path"></param>
  235. /// <returns></returns>
  236. public string GetFileName(string path)
  237. {
  238. var filename = Path.GetFileName(path);
  239. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + filename;
  240. }
  241. }
  242. }