//rendering the dialog box public static void Render(string name, string action, string legend, string label, string onComplete) { var insideView = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page); UploadRepository.CreateUploadGUID(insideView.Context); var uploadid = UploadIDTag + Guid.NewGuid().ToString(); insideView.Html.RenderPartial( "FileUpload", new FileUpload() { Name = name, OnComplete = onComplete, Action = action, Title = legend, Label = label, UploadID = uploadid, MaxFileSize = PrettyPrinter.FormatByteCount((ulong)((HttpRuntimeSection)ConfigurationManager.GetSection("system.web/httpRuntime")).MaxRequestLength * 100) }); }
/// <summary> /// Renders JavaScript to turn the specified file input control into an /// Uploadify upload control. /// </summary> /// <param name="helper"></param> /// <param name="name"></param> /// <param name="action"></param> /// <param name="legend"></param> /// <param name="label"></param> /// <returns></returns> public static MvcHtmlString AjaxUpload(this HtmlHelper helper, string name, string action, string legend, string label) { // string scriptPath = ("~/Content/jqueryPlugins/uploadify/"); StringBuilder sb = new StringBuilder(); //Include the JS file. sb.AppendLine(helper.ScriptInclude("~/Scripts/jquery.form.js").ToString()); sb.AppendLine(helper.ScriptInclude("~/Scripts/jquery.blockUI.js").ToString()); sb.AppendLine(String.Format( "<script type=\"text/javascript\">" + Environment.NewLine + "$(function() {{" + Environment.NewLine + " $(\"#{0}\").ajaxForm({{" + Environment.NewLine + " iframe: true," + Environment.NewLine + " dataType: \"json\"," + Environment.NewLine + "beforeSubmit: function() {{" + Environment.NewLine + "$(\"#{0}\").block({{ message: '<h1><img src=\"" + appPath + "/Content/busy.gif\" /> Uploading file...</h1>' }});" + Environment.NewLine + "}}," + "success: function(result) {{" + Environment.NewLine + "$(\"#{0}\").unblock();" + Environment.NewLine + "$(\"#{0}\").resetForm();" + Environment.NewLine + "$.growlUI('Upload Status',result.message);" + Environment.NewLine + "$(\"#{0}preview\").attr(\"src\",result.thumbnail);" + Environment.NewLine + "HandleType(\"{0}\",result.type,result.thumbnail,result.text,result.path,result.duration);" + Environment.NewLine + "}}," + "error: function(xhr, textStatus, errorThrown) {{" + Environment.NewLine + "$(\"#{0}\").unblock();" + Environment.NewLine + "$(\"#{0}\").resetForm();" + Environment.NewLine + "$.growlUI(null, 'Error uploading file' );" + Environment.NewLine + "}}" + Environment.NewLine + "}});" + Environment.NewLine + "}});" + Environment.NewLine + "</script>", name)) ; //Dump the script to initialze Uploadify TagBuilder uploadFormTagBuilder = new TagBuilder("form"); uploadFormTagBuilder.GenerateId(name); uploadFormTagBuilder.Attributes["action"] = action; uploadFormTagBuilder.Attributes["method"] = "post"; uploadFormTagBuilder.Attributes["enctype"] = "multipart/form-data"; TagBuilder fieldsetBuilder = new TagBuilder("fieldset"); TagBuilder legendBuilder = new TagBuilder("legend"); legendBuilder.InnerHtml = legend; TagBuilder labelBuilder = new TagBuilder("label"); TagBuilder hiddenTagBuilder = new TagBuilder("input"); hiddenTagBuilder.Attributes["type"] = "hidden"; hiddenTagBuilder.Attributes["value"] = UPLOAD_ID_TAG + Guid.NewGuid().ToString(); hiddenTagBuilder.Attributes["name"] = name + "UploadID"; TagBuilder inputBuilder = new TagBuilder("input"); inputBuilder.Attributes["type"] = "file"; inputBuilder.Attributes["name"] = name + "file"; labelBuilder.InnerHtml = label + inputBuilder.ToString() + "(" + PrettyPrinter.FormatByteCount((ulong)((HttpRuntimeSection)ConfigurationManager.GetSection("system.web/httpRuntime")).MaxRequestLength * 100) + " Max Upload Size)" + "<br/>"; //<img id="@(ViewBag.Name)preview" alt="Preview" src="../../Content/check48.png"/> TagBuilder imagePreview = new TagBuilder("img"); imagePreview.GenerateId(name + "preview"); imagePreview.Attributes["src"] = "../../Content/check48.png"; imagePreview.Attributes["alt"] = "Preview"; TagBuilder ajaxUploadButtonBuilder = new TagBuilder("input"); ajaxUploadButtonBuilder.GenerateId(name + "ajaxUploadButton"); ajaxUploadButtonBuilder.Attributes["type"] = "submit"; ajaxUploadButtonBuilder.Attributes["value"] = "Submit"; fieldsetBuilder.InnerHtml = legendBuilder.ToString() + Environment.NewLine + labelBuilder.ToString() + Environment.NewLine + imagePreview.ToString() + Environment.NewLine + hiddenTagBuilder + Environment.NewLine + ajaxUploadButtonBuilder.ToString(); uploadFormTagBuilder.InnerHtml = fieldsetBuilder.ToString(); sb.Append(uploadFormTagBuilder.ToString()); return(MvcHtmlString.Create(sb.ToString())); }