uploaderExtensions.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using Bowin.Web.Controls.Mvc;
  7. namespace System.Web.Mvc
  8. {
  9. public static class UploaderExtensions
  10. {
  11. public static MvcHtmlString UploaderFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper
  12. , Expression<Func<TModel, TProperty>> expression
  13. , IDictionary<string, object> attributes = null
  14. , FileType fileType = FileType.ALL)
  15. {
  16. if (expression == null)
  17. {
  18. throw new ArgumentNullException("expression");
  19. }
  20. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  21. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  22. UrlHelper urlhelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  23. StringBuilder sb = new StringBuilder();
  24. sb.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\r\n", urlhelper.Content("~/Scripts/Bowin.Control.Core/Plugins/uploadifyFile/OwnUploadify.css"));
  25. sb.Append(Bowin.Web.Controls.Mvc.Uploader.CreateControl(new uploaderOption() { Name = fullHtmlFieldName, MainTableID = modelMetadata.Model }, attributes, fileType).Render(urlhelper));
  26. return MvcHtmlString.Create(sb.ToString());
  27. }
  28. public static MvcHtmlString Uploader(this HtmlHelper htmlHelper, uploaderOption _uploaderOption, IDictionary<string, object> attributes = null, FileType fileType = FileType.ALL)
  29. {
  30. StringBuilder sb = new StringBuilder();
  31. UrlHelper urlhelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  32. sb.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\r\n", urlhelper.Content("~/Scripts/Bowin.Control.Core/Plugins/uploadifyFile/OwnUploadify.css"));
  33. sb.Append(Bowin.Web.Controls.Mvc.Uploader.CreateControl(_uploaderOption, attributes, fileType).Render(urlhelper));
  34. return MvcHtmlString.Create(sb.ToString());
  35. }
  36. public static MvcHtmlString Uploader(this HtmlHelper htmlHelper, IDictionary<string, object> attributes = null, FileType fileType = FileType.ALL)
  37. {
  38. StringBuilder sb = new StringBuilder();
  39. UrlHelper urlhelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  40. sb.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\r\n", urlhelper.Content("~/Scripts/Bowin.Control.Core/Plugins/uploadifyFile/OwnUploadify.css"));
  41. sb.Append(Bowin.Web.Controls.Mvc.Uploader.CreateControl(new uploaderOption(), attributes, fileType).Render(urlhelper));
  42. return MvcHtmlString.Create(sb.ToString());
  43. }
  44. public static MvcHtmlString SingleUploaderFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper
  45. , Expression<Func<TModel, TProperty>> expression
  46. , IDictionary<string, object> attributes = null)
  47. {
  48. if (expression == null)
  49. {
  50. throw new ArgumentNullException("expression");
  51. }
  52. //ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  53. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  54. TagBuilder tabBuilder = new TagBuilder("input");
  55. tabBuilder.MergeAttribute("type", "file");
  56. tabBuilder.MergeAttribute("name", fullHtmlFieldName);
  57. if (attributes != null)
  58. {
  59. tabBuilder.MergeAttributes(attributes);
  60. }
  61. return MvcHtmlString.Create(tabBuilder.ToString(TagRenderMode.SelfClosing));
  62. }
  63. }
  64. }