将HTML文件渲染到视图中 - MVC 5

时间:2014-04-15 15:09:54

标签: asp.net-mvc razor rendering

我上传了一个带有Dropzone.js的Html文件,作为网站示例:

//Upload view
@using System.Text;
@{
    Layout = null;
    string path = ViewBag.path;
}
@if (String.IsNullOrEmpty(fileName))
{
    @Styles.Render("~/Content/css")
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/dropzone")
    <form action="@Url.Action("Upload", "Home")" class="dropzone" id="dropzoneJsForm"></form>
    <button id="submit-all">Submit All Files</button>

    <script type="text/javascript">
        Dropzone.options.dropzoneJsForm = {
            //prevents Dropzone from uploading dropped files immediately
            autoProcessQueue: false,
            init: function () {
                var submitButton = document.querySelector("#submit-all");
                var myDropzone = this; //closure
                submitButton.addEventListener("click", function () {
                    //tell Dropzone to process all queued files
                    myDropzone.processQueue();
                });

            }
        };

    </script>
}
else
{
    @Html.Raw(System.Web.HttpUtility.HtmlEncode(File.ReadAllText(path)))
}

//HomeController
public ActionResult Upload()
       {

           foreach (string fileName in Request.Files)
           {
               HttpPostedFileBase file = Request.Files[fileName];
               if (file.ContentLength > 0)
               {
                   var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                   file.SaveAs(path);
                   ViewBag.path = path;
               }
           }
           return View();

       }

文件已保存,视图可以读取但页面没有显示任何内容。 我无法弄清楚什么不起作用。如果我激活调试器似乎一切正常,但最后页面没有绘制文件的html标记。你能帮助我或建议一个可能的解决方案

非常感谢

1 个答案:

答案 0 :(得分:2)

相反HttpUtility.HtmlDecode,请你检查 HttpUtility.UrlDecode ,假设你的文件是纯HTML,而不是html字符。