uploader.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public class Uploader
  9. {
  10. public string Name { get; set; }
  11. public string Title { get; set; }
  12. public object Value { get; set; }
  13. public Dictionary<string, object> Attributes { get; set; }
  14. public FileType FileType { get; set; }
  15. public bool? IsEnabled { get; set; }
  16. public int? MaxFileSize { get; set; }
  17. public string UploadUrl { get; set; }
  18. public Uploader()
  19. {
  20. }
  21. public string Render(UrlHelper Url)
  22. {
  23. TagBuilder tabBuilder = new TagBuilder("fieldset");
  24. tabBuilder.MergeAttribute("name", Name);
  25. tabBuilder.MergeAttribute("data-uploader", "");
  26. tabBuilder.MergeAttribute("data-value", (Value ?? "").ToString());
  27. tabBuilder.MergeAttribute("data-type", ((int)FileType).ToString());
  28. tabBuilder.MergeAttribute("data-rooturl", Url.Content("~/"));
  29. if (this.MaxFileSize.HasValue)
  30. {
  31. tabBuilder.MergeAttribute("maxFileSize", this.MaxFileSize.Value.ToString());
  32. }
  33. if (!string.IsNullOrEmpty(UploadUrl))
  34. {
  35. tabBuilder.MergeAttribute("data-uploadurl", Url.Content(UploadUrl));
  36. }
  37. tabBuilder.MergeAttributes(Attributes);
  38. StringBuilder sb = new StringBuilder();
  39. sb.AppendLine("<legend>");
  40. sb.AppendFormat("<input type=\"image\" name=\"uploader_btn_add\" src=\'{1}' onclick=\"uploader_showFileDialog('{0}');return false;\" " + (IsEnabled == false ? "disabled=\"disabled\"" : "") + " />\r\n", Name, Url.Content("~/Scripts/Bowin.Control.Core/Plugins/uploadifyFile/add.png"));
  41. sb.AppendLine("&nbsp;" + (Title ?? "附件上传") + " </legend>");
  42. sb.AppendLine("<div style=\"position: relative; vertical-align: bottom;\">");
  43. sb.AppendLine("<ul name=\"uploader_ul\" class=\"uploader_ul\">");
  44. sb.AppendLine("</ul>");
  45. sb.AppendLine("</div>");
  46. tabBuilder.InnerHtml = sb.ToString();
  47. return tabBuilder.ToString(TagRenderMode.Normal);
  48. }
  49. public static Uploader CreateControl(uploaderOption uOption, IDictionary<string, object> attributes = null, FileType fileType = FileType.ALL)
  50. {
  51. Uploader uploader = new Uploader();
  52. uploader.Name = uOption.Name;
  53. uploader.Title = uOption.Title;
  54. uploader.Value = uOption.MainTableID == null ? Guid.Empty.ToString() : uOption.MainTableID;
  55. uploader.FileType = fileType == FileType.ALL ? uOption.FileType : fileType;
  56. uploader.IsEnabled = uOption.IsEnabled;
  57. uploader.MaxFileSize = uOption.MaxFileSize;
  58. uploader.UploadUrl = uOption.UploadUrl;
  59. return uploader;
  60. }
  61. }
  62. }