PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/FileManager/FileUpload.ashx

#
Unknown | 105 lines | 90 code | 15 blank | 0 comment | 0 complexity | 30c6fae8f89977b7179de07a76af29a2 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <%@ WebHandler Language="C#" Class="FileUpload" %>
  2. using System;
  3. using System.Web;
  4. using System.Web.Hosting;
  5. using System.Linq;
  6. using System.IO;
  7. using System.Web.SessionState;
  8. using BlogEngine.Core;
  9. using BlogEngine.Core.Providers;
  10. public class FileUpload : IHttpHandler, IRequiresSessionState {
  11. public enum FileType
  12. {
  13. Image,
  14. File
  15. }
  16. public void ProcessRequest(HttpContext context)
  17. {
  18. var files = context.Request.Files;
  19. if (files.Count == 0)
  20. {
  21. context.Response.Write("0:Unknown file upload");
  22. context.Response.End();
  23. }
  24. var file = files[0];
  25. var dirName = string.Format("/{0}/{1}", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"));
  26. var dir = BlogService.GetDirectory(dirName);
  27. var uFile = BlogService.UploadFile(file.InputStream, file.FileName, dir, true);
  28. if (uFile.IsImage)
  29. {
  30. var imgString = string.Format("<img src=\"{0}\" />", uFile.AsImage.ImageUrl);
  31. context.Response.Write(string.Format("1:{0}:{1}:{2}", imgString, uFile.Name, uFile.ParentDirectory.FullPath));
  32. context.Response.End();
  33. }
  34. else
  35. {
  36. var fileString = string.Format("<p><a href=\"{0}\" >{1}</a></p>", uFile.FileDownloadPath, uFile.FileDescription);
  37. context.Response.Write(string.Format("1:{0}:{1}:{2}", fileString, uFile.Name, uFile.ParentDirectory.FullPath));
  38. context.Response.End();
  39. }
  40. context.Response.Write("0:Upload failed..");
  41. }
  42. public bool IsReusable {
  43. get {
  44. return false;
  45. }
  46. }
  47. private FileType ExtractType(string Extension)
  48. {
  49. if (Extension == ".png" ||
  50. Extension == ".jpg" ||
  51. Extension == ".jpeg" ||
  52. Extension == ".bmp" ||
  53. Extension == ".gif")
  54. return FileType.Image;
  55. else
  56. return FileType.File;
  57. }
  58. /// <summary>
  59. /// Sizes the format.
  60. /// </summary>
  61. /// <param name="size">
  62. /// The string size.
  63. /// </param>
  64. /// <param name="formatString">
  65. /// The format string.
  66. /// </param>
  67. /// <returns>
  68. /// The string.
  69. /// </returns>
  70. private static string SizeFormat(float size, string formatString)
  71. {
  72. if (size < 1024)
  73. {
  74. return string.Format("{0} bytes", size.ToString(formatString));
  75. }
  76. if (size < Math.Pow(1024, 2))
  77. {
  78. return string.Format("{0} kb", (size / 1024).ToString(formatString));
  79. }
  80. if (size < Math.Pow(1024, 3))
  81. {
  82. return string.Format("{0} mb", (size / Math.Pow(1024, 2)).ToString(formatString));
  83. }
  84. if (size < Math.Pow(1024, 4))
  85. {
  86. return string.Format("{0} gb", (size / Math.Pow(1024, 3)).ToString(formatString));
  87. }
  88. return size.ToString(formatString);
  89. }
  90. }