UpLoadFiles.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.IO;
  3. namespace Ant.Service.Utilities
  4. {
  5. /// <summary>
  6. /// UpLoadFiles 的摘要说明
  7. /// </summary>
  8. public class UpLoadFiles : System.Web.UI.Page
  9. {
  10. public UpLoadFiles()
  11. {
  12. //
  13. // TODO: 在此处添加构造函数逻辑
  14. //
  15. }
  16. public string UploadFile(string filePath, int maxSize, string[] fileType, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
  17. {
  18. string Result = "UnDefine";
  19. bool typeFlag = false;
  20. string FilePath = filePath;
  21. int MaxSize = maxSize;
  22. string strFileName, strNewName, strFilePath;
  23. if (TargetFile.PostedFile.FileName == "")
  24. {
  25. return "FILE_ERR";
  26. }
  27. strFileName = TargetFile.PostedFile.FileName;
  28. TargetFile.Accept = "*/*";
  29. strFilePath = FilePath;
  30. if (Directory.Exists(strFilePath) == false)
  31. {
  32. Directory.CreateDirectory(strFilePath);
  33. }
  34. FileInfo myInfo = new FileInfo(strFileName);
  35. string strOldName = myInfo.Name;
  36. strNewName = strOldName.Substring(strOldName.LastIndexOf("."));
  37. strNewName = strNewName.ToLower();
  38. if (TargetFile.PostedFile.ContentLength <= MaxSize)
  39. {
  40. for (int i = 0; i <= fileType.GetUpperBound(0); i++)
  41. {
  42. if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = true; break; }
  43. }
  44. if (typeFlag)
  45. {
  46. string strFileNameTemp = GetUploadFileName();
  47. string strFilePathTemp = strFilePath;
  48. float strFileSize = TargetFile.PostedFile.ContentLength;
  49. strOldName = strFileNameTemp + strNewName;
  50. strFilePath = strFilePath + "\\" + strOldName;
  51. TargetFile.PostedFile.SaveAs(strFilePath);
  52. Result = strOldName + "|" + strFileSize;
  53. TargetFile.Dispose();
  54. }
  55. else
  56. {
  57. return "TYPE_ERR";
  58. }
  59. }
  60. else
  61. {
  62. return "SIZE_ERR";
  63. }
  64. return (Result);
  65. }
  66. /// <summary>
  67. /// 上传文件
  68. /// </summary>
  69. /// <param name="filePath">保存文件地址</param>
  70. /// <param name="maxSize">文件最大大小</param>
  71. /// <param name="fileType">文件后缀类型</param>
  72. /// <param name="TargetFile">控件名</param>
  73. /// <param name="saveFileName">保存后的文件名和地址</param>
  74. /// <param name="fileSize">文件大小</param>
  75. /// <returns></returns>
  76. public string UploadFile(string filePath, int maxSize, string[] fileType, System.Web.UI.HtmlControls.HtmlInputFile TargetFile, out string saveFileName, out int fileSize)
  77. {
  78. saveFileName = "";
  79. fileSize = 0;
  80. string Result = "";
  81. bool typeFlag = false;
  82. string FilePath = filePath;
  83. int MaxSize = maxSize;
  84. string strFileName, strNewName, strFilePath;
  85. if (TargetFile.PostedFile.FileName == "")
  86. {
  87. return "请选择上传的文件";
  88. }
  89. strFileName = TargetFile.PostedFile.FileName;
  90. TargetFile.Accept = "*/*";
  91. strFilePath = FilePath;
  92. if (Directory.Exists(strFilePath) == false)
  93. {
  94. Directory.CreateDirectory(strFilePath);
  95. }
  96. FileInfo myInfo = new FileInfo(strFileName);
  97. string strOldName = myInfo.Name;
  98. strNewName = strOldName.Substring(strOldName.LastIndexOf("."));
  99. strNewName = strNewName.ToLower();
  100. if (TargetFile.PostedFile.ContentLength <= MaxSize)
  101. {
  102. string strFileNameTemp = GetUploadFileName();
  103. string strFilePathTemp = strFilePath;
  104. strOldName = strFileNameTemp + strNewName;
  105. strFilePath = strFilePath + "\\" + strOldName;
  106. fileSize = TargetFile.PostedFile.ContentLength / 1024;
  107. saveFileName = strFilePath.Substring(strFilePath.IndexOf("FileUpload\\"));
  108. TargetFile.PostedFile.SaveAs(strFilePath);
  109. TargetFile.Dispose();
  110. }
  111. else
  112. {
  113. return "上传文件超出指定的大小";
  114. }
  115. return (Result);
  116. }
  117. public string UploadFile(string filePath, int maxSize, string[] fileType, string filename, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
  118. {
  119. string Result = "UnDefine";
  120. bool typeFlag = false;
  121. string FilePath = filePath;
  122. int MaxSize = maxSize;
  123. string strFileName, strNewName, strFilePath;
  124. if (TargetFile.PostedFile.FileName == "")
  125. {
  126. return "FILE_ERR";
  127. }
  128. strFileName = TargetFile.PostedFile.FileName;
  129. TargetFile.Accept = "*/*";
  130. strFilePath = FilePath;
  131. if (Directory.Exists(strFilePath) == false)
  132. {
  133. Directory.CreateDirectory(strFilePath);
  134. }
  135. FileInfo myInfo = new FileInfo(strFileName);
  136. string strOldName = myInfo.Name;
  137. strNewName = strOldName.Substring(strOldName.Length - 3, 3);
  138. strNewName = strNewName.ToLower();
  139. if (TargetFile.PostedFile.ContentLength <= MaxSize)
  140. {
  141. for (int i = 0; i <= fileType.GetUpperBound(0); i++)
  142. {
  143. if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = true; break; }
  144. }
  145. if (typeFlag)
  146. {
  147. string strFileNameTemp = filename;
  148. string strFilePathTemp = strFilePath;
  149. strOldName = strFileNameTemp + "." + strNewName;
  150. strFilePath = strFilePath + "\\" + strOldName;
  151. TargetFile.PostedFile.SaveAs(strFilePath);
  152. Result = strOldName;
  153. TargetFile.Dispose();
  154. }
  155. else
  156. {
  157. return "TYPE_ERR";
  158. }
  159. }
  160. else
  161. {
  162. return "SIZE_ERR";
  163. }
  164. return (Result);
  165. }
  166. public string GetUploadFileName()
  167. {
  168. string Result = "";
  169. DateTime time = DateTime.Now;
  170. Result += time.Year.ToString() + FormatNum(time.Month.ToString(), 2) + FormatNum(time.Day.ToString(), 2) + FormatNum(time.Hour.ToString(), 2) + FormatNum(time.Minute.ToString(), 2) + FormatNum(time.Second.ToString(), 2) + FormatNum(time.Millisecond.ToString(), 3);
  171. return (Result);
  172. }
  173. public string FormatNum(string Num, int Bit)
  174. {
  175. int L;
  176. L = Num.Length;
  177. for (int i = L; i < Bit; i++)
  178. {
  179. Num = "0" + Num;
  180. }
  181. return (Num);
  182. }
  183. }
  184. }