private MvcHtmlString RenderHtmlEditorInternal <TModel>(HtmlHelper <TModel> html, IField field, string label, string containerID, IDictionary <string, object> uploadOptions, FilePresentOptions presentOptions) { var divBuilder = new TagBuilder("div"); divBuilder.InnerHtml = label; divBuilder.MergeAttribute("id", containerID); divBuilder.AddCssClass("FileUploadField"); if (!string.IsNullOrEmpty(field.alias)) { divBuilder.AddCssClass($"FileUploadField_{field.alias}"); divBuilder.AddCssClass($"FieldAlias_{field.alias}"); } var str = $@" <script type='text/javascript'>$(function(){{ $('div#FileUploadField_Files_{containerID} input.buttonDelete').unbind('click').click(function(){{ $(this).parent().remove(); }}); var {containerID} = $('#{containerID}').requestFileUploadSingle({Newtonsoft.Json.JsonConvert.SerializeObject(uploadOptions)}); {(!string.IsNullOrEmpty(field.alias) ? $"FileUploadField_{field.alias} = {containerID};" : "")} {containerID}.getElement().bind('requestFileUploadSingleAfter', function(e, result, message, data) {{" + $@"{containerID}.reset();" + $@"if (result == JsonResult.OK) {{" + $@"$('div#FileUploadField_Files_{containerID}').{(field.IsMultipleValues ? "append" : "html")}(""" + $@"<span class='FileUploadField' data-idfile='"" + data + ""'>" + $@"<input type='button' class='buttonDelete' value='X'> " + $@"<input type='checkbox' checked name='fieldValue_{field.IdField}[]' value='"" + data + ""'> " + $@"<label>файл №"" + data + ""</label>" + $@"</span>" + $@""");" + $@"$('div#FileUploadField_Files_{containerID} input.buttonDelete').unbind('click').click(function(){{ $(this).parent().remove(); }});" + $@"}}" + $@"}}); }});</script> {divBuilder.ToString(TagRenderMode.Normal)} "; str += $"<div id='FileUploadField_Files_{containerID}'>"; if (field is FieldData) { var fieldData = field as FieldData; var IdList = fieldData.Where(x => x is int).Select(x => (int)x).ToList(); //var filesList = GetDB().Repo1.Where(x => IdList.Contains(x.IdFile)).ToDictionary(x => x.IdFile, x => x); foreach (var IdFile in IdList) { //var file = filesList.GetValueOrDefault(IdFile); //str += $"<span class='FileUploadField {(file == null ? "FileNotFound" : "")}' data-idfile='{IdFile}'>"; str += $"<span class='FileUploadField' data-idfile='{IdFile}'>"; str += "<input type='button' class='buttonDelete' value='X'> "; str += $"<input type='checkbox' checked name='fieldValue_{field.IdField}[]' value='{IdFile}'> "; //str += $"<label>{file?.NameFile ?? "Файл не найден"}</label>"; str += $"<label>{IdFile}</label>"; str += "</span>"; } } str += $"</div><input type='hidden' name='fieldValue_{field.IdField}[]' value=''>"; return(MvcHtmlString.Create(str)); }
MvcHtmlString ICustomFieldRender <FileFieldType> .RenderHtmlEditor <TModel>(HtmlHelper <TModel> html, IField field, IDictionary <string, object> htmlAttributes, params object[] additionalParameters) { var label = htmlAttributes?.GetValueOrDefault("label", "Выберите файл")?.ToString() ?? "Выберите файл"; var containerID = $"FileUploadField_{field.IdField}_{new Random().Next(100000, 999999)}"; var presentOptions = additionalParameters.FirstOrDefault() as FilePresentOptions; if (presentOptions == null) { presentOptions = new FilePresentOptions(); } var optionsFromAttributes = htmlAttributes?.GetValueOrDefault("uploadOptions"); if (optionsFromAttributes != null) { var typeDictionary = OnUtils.Types.TypeHelpers.ExtractGenericInterface(optionsFromAttributes.GetType(), typeof(IDictionary <,>)); if (typeDictionary != null) { var dictSimple = (optionsFromAttributes as System.Collections.IDictionary); if (typeDictionary.GenericTypeArguments[0] != typeof(string) || typeDictionary.GenericTypeArguments[1] != typeof(object) || dictSimple.IsReadOnly) { var newUploadOptions = new Dictionary <string, object>(); foreach (var key in dictSimple.Keys) { newUploadOptions[key.ToString()] = $"{dictSimple[key]}"; } optionsFromAttributes = newUploadOptions; } } else { optionsFromAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(optionsFromAttributes); } } else { optionsFromAttributes = new Dictionary <string, object>(); } var uploadOptions = optionsFromAttributes as IDictionary <string, object>; uploadOptions["formData"] = new { moduleName = "" }; var module = html.ViewDataContainer.GetModule(); if (module == null) { throw new InvalidProgramException(); } if (module != null) { uploadOptions["formData"] = new { moduleName = module.UrlName } } ; uploadOptions["multiple"] = field.IsMultipleValues; return(RenderHtmlEditorInternal <TModel>(html, field, label, containerID, uploadOptions, presentOptions)); }
private MvcHtmlString RenderHtmlEditorInternal <TModel>(HtmlHelper <TModel> html, IField field, string label, string containerID, IDictionary <string, object> uploadOptions, FilePresentOptions presentOptions) { uploadOptions["allowedTypes"] = "jpg, jpeg, png, gif, bmp"; uploadOptions["acceptFiles"] = "image/"; var divBuilder = new TagBuilder("div"); divBuilder.InnerHtml = label; divBuilder.MergeAttribute("id", containerID); divBuilder.AddCssClass("FileUploadField"); if (!string.IsNullOrEmpty(field.alias)) { divBuilder.AddCssClass($"FileUploadField_{field.alias}"); divBuilder.AddCssClass($"FieldAlias_{field.alias}"); } var imagePresentOptions = presentOptions as ImagePresentOptions; var htmlSizes = new List <string>(); if (imagePresentOptions != null && imagePresentOptions.MaxWidth.HasValue) { htmlSizes.Add($"max-width:{imagePresentOptions.MaxWidth}px;width: expression(this.width > {imagePresentOptions.MaxWidth } ? {imagePresentOptions.MaxWidth}: true);"); } if (imagePresentOptions != null && imagePresentOptions.MaxHeight.HasValue) { htmlSizes.Add($"max-height:{imagePresentOptions.MaxHeight}px;height: expression(this.height > {imagePresentOptions.MaxHeight } ? {imagePresentOptions.MaxHeight}: true);"); } var htmlSizesAttrs = "style='" + string.Join("; ", htmlSizes) + "'"; var str = $@" <script type='text/javascript'>$(function(){{ $('div#FileUploadField_Files_{containerID} input.buttonDelete').unbind('click').click(function(){{ $(this).parent().remove(); }}); var {containerID} = $('#{containerID}').requestFileUploadSingle({Newtonsoft.Json.JsonConvert.SerializeObject(uploadOptions)}); {(!string.IsNullOrEmpty(field.alias) ? $"FileUploadField_{field.alias} = {containerID};" : "")} {containerID}.getElement().bind('requestFileUploadSingleAfter', function(e, result, message, data) {{ " + $"{containerID}.reset();" + $"if (result == JsonResult.OK) {{" + $@"$('div#FileUploadField_Files_{containerID}').{(field.IsMultipleValues ? "append" : "html")}(" + $@"""<span class='FileUploadField' data-idfile='"" + data + ""'>" + $@"<input type='button' class='buttonDelete' value='X'> " + $@"<input type='checkbox' checked name='fieldValue_{field.IdField}[]' value='"" + data + ""'> " + $@"<img src='/fm/File/"" + data + ""' {htmlSizesAttrs} />" + $@"</span>""" + $@");" + $@"$('div#FileUploadField_Files_{containerID} input.buttonDelete').unbind('click').click(function(){{ $(this).parent().remove(); }});" + $@"}}" + $@"}}); }});</script> {divBuilder.ToString(TagRenderMode.Normal)} "; str += $"<div id='FileUploadField_Files_{containerID}'>"; if (field is FieldData) { var fieldData = field as FieldData; var IdList = fieldData.Where(x => x is int).Select(x => (int)x).ToList(); //var filesList = GetDB().Repo1.Where(x => IdList.Contains(x.IdFile)).ToDictionary(x => x.IdFile, x => x); foreach (var IdFile in IdList) { //var file = filesList.GetValueOrDefault(IdFile); //str += $"<span class='FileUploadField {(file == null ? "FileNotFound" : "")}' data-idfile='{IdFile}'>"; str += $"<span class='FileUploadField' data-idfile='{IdFile}'>"; str += "<input type='button' class='buttonDelete' value='X'> "; str += $"<input type='checkbox' checked name='fieldValue_{field.IdField}[]' value='{IdFile}'> "; //str += $"<label>{file?.NameFile ?? "Файл не найден"}</label>"; str += $"<img src='/fm/image/{IdFile}' {htmlSizesAttrs} />"; str += "</span>"; } } str += $"</div><input type='hidden' name='fieldValue_{field.IdField}[]' value=''>"; return(MvcHtmlString.Create(str)); }
public static MvcHtmlString FileUploadFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes, FilePresentOptions presentOptions) { return(htmlHelper.EditorFor(expression, "FileUpload", new { htmlAttributes = htmlAttributes, presentOptions = presentOptions })); }
public static MvcHtmlString FileUploadFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes, FilePresentOptions presentOptions) { return(htmlHelper.FileUploadFor(expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), presentOptions)); }
public static MvcHtmlString FileUploadFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, FilePresentOptions presentOptions) { return(htmlHelper.FileUploadFor(expression, null, presentOptions)); }