RoadFlow2.1 临时演示

Upload.ashx.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. namespace WebForm.Controls.UploadFiles
  7. {
  8. /// <summary>
  9. /// Upload 的摘要说明
  10. /// </summary>
  11. public class Upload : IHttpHandler
  12. {
  13. public void ProcessRequest(HttpContext context)
  14. {
  15. context.Response.ContentType = "text/plain";
  16. string str1 = context.Request.Form["str1"];
  17. string str2 = context.Request.Form["str2"];
  18. string filetype = context.Request.Form["filetype"];
  19. var obj = RoadFlow.Cache.IO.Opation.Get(str1 ?? "");
  20. if (str1.IsNullOrEmpty() || str2.IsNullOrEmpty() || obj == null || obj.ToString() != str2)
  21. {
  22. context.Response.Write("您不能上传文件");
  23. return;
  24. }
  25. //接收上传后的文件
  26. HttpPostedFile file = context.Request.Files["Filedata"];
  27. if (filetype.IsNullOrEmpty())
  28. {
  29. if (!RoadFlow.Utility.Config.UploadFileType.Contains(Path.GetExtension(file.FileName).TrimStart('.'), StringComparison.CurrentCultureIgnoreCase))
  30. {
  31. context.Response.Write("您上传的文件类型不被允许");
  32. return;
  33. }
  34. }
  35. else
  36. {
  37. if (!filetype.Contains(Path.GetExtension(file.FileName).TrimStart('.'), StringComparison.CurrentCultureIgnoreCase))
  38. {
  39. context.Response.Write("您上传的文件类型不被允许");
  40. return;
  41. }
  42. }
  43. //获取文件的保存路径
  44. string uploadPath;
  45. string uploadFullPath = context.Server.MapPath(getFilePath(out uploadPath));
  46. //判断上传的文件是否为空
  47. if (file != null)
  48. {
  49. if (!Directory.Exists(uploadFullPath))
  50. {
  51. Directory.CreateDirectory(uploadFullPath);
  52. }
  53. //保存文件
  54. string newFileName = getFileName(uploadFullPath, file.FileName);
  55. string newFileFullPath = uploadFullPath + newFileName;
  56. try
  57. {
  58. int fileLength = file.ContentLength;
  59. file.SaveAs(newFileFullPath);
  60. context.Response.Write("1|" + (uploadPath + newFileName) + "|" + (fileLength / 1000).ToString("###,###") + "|" + newFileName);
  61. return;
  62. }
  63. catch
  64. {
  65. context.Response.Write("上传文件发生了错误");
  66. return;
  67. }
  68. }
  69. else
  70. {
  71. context.Response.Write("上传文件为空");
  72. return;
  73. }
  74. }
  75. /// <summary>
  76. /// 得到上传文件名
  77. /// </summary>
  78. /// <param name="fileName"></param>
  79. /// <returns></returns>
  80. private string getFileName(string filePath, string fileName)
  81. {
  82. while (System.IO.File.Exists(filePath + fileName))
  83. {
  84. fileName = Path.GetFileNameWithoutExtension(fileName) + "_" + RoadFlow.Utility.Tools.GetRandomString() + Path.GetExtension(fileName);
  85. }
  86. return fileName;
  87. }
  88. /// <summary>
  89. /// 得到文件保存路径
  90. /// </summary>
  91. /// <returns></returns>
  92. private string getFilePath(out string path1)
  93. {
  94. DateTime date = RoadFlow.Utility.DateTimeNew.Now;
  95. path1 = WebForm.Common.Tools.BaseUrl + "/Files/UploadFiles/" + date.ToString("yyyyMM") + "/" + date.ToString("dd") + "/";
  96. return path1;
  97. }
  98. public bool IsReusable
  99. {
  100. get
  101. {
  102. return false;
  103. }
  104. }
  105. }
  106. }