“/'应用程序中的服务器错误”上传图像时

时间:2016-12-12 16:41:12

标签: c# asp.net-mvc photo-upload

我想将个人资料照片上传到我的系统。但是,我点击上传,“服务器错误在'/'应用程序”消息出现。另外我已经发现URL有点不同,正确的URL应该是这样的。
/ ProfileController可/ UploadPhoto
但是这里的URL是 / admin / ProfileController / UploadPhoto
应该怎么做才能使这项工作? 这是我在控制器中的代码

[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase file)
{
     if (file != null && file.ContentLength > 0)
     {
            var user = Session["userID"].ToString();
            var fileExt = Path.GetExtension(file.FileName);
            var fileName = user + ".png";

            if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg"))
            {
                 var filePath = HostingEnvironment.MapPath("~/Content/images/profile/") + fileName;
                var directory = new DirectoryInfo(HostingEnvironment.MapPath("~/Content/images/profile/"));

                if (directory.Exists == false)
                {
                     directory.Create();
                }
                ViewBag.FilePath = filePath.ToString();
                file.SaveAs(filePath);
                return RedirectToAction("Index", new { Message = ManageMessageId.PhotoUploadSuccess });

            }
            else
            {
                  return RedirectToAction("Index", new { Message = ManageMessageId.FileExtensionError });

            }
     }
     return RedirectToAction("Index", new { Message = ManageMessageId.Error }); 
}             

这是视图中的代码

<dl class="dl-horizontal">
        <dd>
            @if (User != null)
            {
                 var imgUrl = Url.Content("Content/Images/" + User + ".png") + "?time=" + DateTime.Now.ToString();
                <div class="input-field col s12">
                    <div class="input-field col s12">
                        <img src="@imgUrl" height="250" width="250" />
                    </div>
                    <div class="mngimg">
                        @using (Html.BeginForm("UploadPhoto", "ProfileController", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            <div class="input-field col s12">
                                <input type="file" name="file" id="files" onchange="this.form.submit()" />
                            </div>
                        }
                    </div>
                </div>
            }
        </dd>
    </dl>

Image of the error I get

RouteConfig.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "AdminPanel",
            "admin/{controller}/{action}/{id}",
            new {`controller="Home", action = "Index", id = UrlParameter.Optional },
            new[] { "OnlineElection.Controllers.Admin" } );

routes.MapRoute(
            name:"Default",
            url:"{controller}/{action}/{id}",
            defaults:new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces:new[] { "OnlineElection.Controllers" }
        );

1 个答案:

答案 0 :(得分:1)

您发布的表单网址无效。

使用控制器时,省略Controller字,仅使用控制器的名称,在这种情况下,ProfileController只变为Profile,这也是URL的重要性

你的Html.BeginForm实际上应该是 @using (Html.BeginForm("UploadPhoto", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" }))