FileUp.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Web.UI.WebControls;
  5. namespace Ant.Service.Utilities
  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 fileName, string fileType)
  115. {
  116. FileStream fileStream = null;
  117. MemoryStream m = new MemoryStream(binData);
  118. try
  119. {
  120. string savePath = HttpContext.Current.Server.MapPath("~/File/");
  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. }
  136. }