Aucune description

UpLoadFiles.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace XYFDRQ.Common
  7. {
  8. public class UpLoadFiles
  9. {
  10. /// <summary>
  11. /// 文件上传基类
  12. /// </summary>
  13. public class upfile
  14. {
  15. private string path = null;
  16. private string fileType = null;
  17. private int sizes = 0;
  18. /// <summary>
  19. /// 初始化变量
  20. /// </summary>
  21. public upfile()
  22. {
  23. path = ""; //上传路径
  24. fileType = ""; //上传文件类型限制
  25. sizes = 1024 * 1024; //传文件的大小,默认1024KB
  26. }
  27. /// <summary>
  28. /// 初始化变量
  29. /// </summary>
  30. /// <param name="path">文件上传路径</param>
  31. /// <param name="fileType">文件上传类型</param>
  32. /// <param name="sizes">文件限制大小</param>
  33. public upfile(string path, string fileType, int sizes)
  34. {
  35. this.Path = path;
  36. this.FileType = fileType;
  37. this.Sizes = sizes;
  38. }
  39. /// <summary>
  40. /// 设置上传路径,如:uploadimages/
  41. /// </summary>
  42. public string Path
  43. {
  44. set
  45. {
  46. path = "/" + value + "/";
  47. }
  48. }
  49. /// <summary>
  50. /// 设置上传文件大小,单位为KB
  51. /// </summary>
  52. public int Sizes
  53. {
  54. set
  55. {
  56. sizes = value * 1024;
  57. }
  58. }
  59. /// <summary>
  60. /// 设置上传文件的类型,如:jpg|gif|bmp
  61. /// </summary>
  62. public string FileType
  63. {
  64. set
  65. {
  66. fileType = value;
  67. }
  68. }
  69. /// <summary>
  70. /// 文件上传基类
  71. /// </summary>
  72. /// <param name="name">控件名称</param>
  73. /// <param name="dept">部门名称</param>
  74. /// <returns>文件路径</returns>
  75. public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile name, string dept)
  76. {
  77. string filePath = null; long strLen = 0;
  78. try
  79. {
  80. //待文件上传路径
  81. string upLoadPath = null;
  82. //所属以当前月份的文件夹名称
  83. string monthFiles = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
  84. string mappathinfo = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + path;
  85. if (!Directory.Exists(mappathinfo + dept + "\\" + monthFiles.Trim()))
  86. {
  87. Directory.CreateDirectory(mappathinfo + dept + "\\" + monthFiles.Trim());
  88. }
  89. upLoadPath = mappathinfo + dept + "\\" + monthFiles.Trim();
  90. //获得文件的上传的路径
  91. string sourcePath = name.Value.Trim();
  92. //判断上传文件是否为空
  93. if (sourcePath == "" || sourcePath == null)
  94. {
  95. message("您没有上传数据!");
  96. return null;
  97. }
  98. int start = sourcePath.LastIndexOf("\\") + 1;
  99. int end = sourcePath.Length;
  100. string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + sourcePath.Substring(start, end - start);
  101. filePath = upLoadPath + "\\" + filename;
  102. FileInfo _file = new FileInfo(filePath);
  103. if (_file.Exists)
  104. {
  105. message("该文件已存在,请查验或修改文件名称");
  106. return null;
  107. }
  108. // 获得文件扩展名
  109. string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
  110. //获得上传文件的大小
  111. strLen = name.PostedFile.ContentLength;
  112. //分解允许上传文件的格式
  113. string[] temp = fileType.Split('|');
  114. //设置上传的文件是否是允许的格式
  115. bool flag = false;
  116. //判断上传文件大小
  117. if (strLen >= sizes)
  118. {
  119. message("上传的数据不能大于" + sizes + "KB");
  120. return null;
  121. }
  122. //判断上传的文件是否是允许的格式
  123. foreach (string data in temp)
  124. {
  125. if (data == tFileType)
  126. {
  127. flag = true;
  128. break;
  129. }
  130. }
  131. //如果为真允许上传,为假则不允许上传
  132. if (!flag)
  133. {
  134. message("目前本系统支持的格式为:" + fileType);
  135. return null;
  136. }
  137. name.PostedFile.SaveAs(filePath);
  138. filePath = monthFiles + "/" + filename;
  139. //message("上传成功");
  140. }
  141. catch
  142. {
  143. //异常
  144. message("出现未知错误!");
  145. return null;
  146. }
  147. return filePath + "," + strLen;
  148. }
  149. private void message(string msg, string url)
  150. {
  151. System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
  152. }
  153. private void message(string msg)
  154. {
  155. System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
  156. }
  157. }
  158. }
  159. }