简体   繁体   English

HttpPostedFileBase始终返回null ASP.NET MVC 5

[英]HttpPostedFileBase always return null ASP.NET MVC 5

I have a problem when i try to upload a file in ASP.NET MVC5. 我尝试在ASP.NET MVC5中上传文件时遇到问题。

Somehow "HttpPostedFileBase file" always returns null, i can't find my problem. 不知何故,“ HttpPostedFileBase文件”始终返回null,我找不到我的问题。

My Code: (Controller) 我的代码:(控制器)

 public ActionResult Edit(PMNieuwePloeg ploeg, FormCollection frm, HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("/Content/ImagesPloegen/"), fileName);
            file.SaveAs(path);

            ploeg.NieuwPloeg.ImageUrl = file.FileName;
        }
        else
        {
            ploeg.NieuwPloeg.ImageUrl = "/geen.jpg";
        }

        if(ploeg.NieuwPloeg.LandID > 0)
        {
            ploeg.NieuwPloeg.UserId = UserManager.FindByName(User.Identity.Name).Id;
            ploeg.NieuwPloeg.UserName = UserManager.FindByName(User.Identity.Name).UserName;
            repoPloegen.Edit(ploeg.NieuwPloeg);

        }
        return RedirectToAction("Index");

    }

View 视图

@using (Html.BeginForm("Edit","Ploegen", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>Ploeg</h4>


    <div class="form-group">
        @Html.LabelFor(model => model.NieuwPloeg.ImageUrl, "Ploeg Image", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="file" required />

        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>

}

Can anyone on here spot the problem? 有人可以在这里发现问题吗?

in Controller 在控制器中

 HttpPostedFileBase casteCertificate = Request.Files["UploadRegCard"];

in view 鉴于

@using (Html.BeginForm("ActionMethod", "Controller", FormMethod.Post, new { enctype = "multipart/form-data", name = "myForm", id = "myForm" }))
    {
     <input type="file" name="UploadRegCard" id="UploadRegCard" class="form-control" />
}

using name attribute of input element you can retrive file from view to controller 使用输入元素的名称属性,您可以从视图到控制器检索文件

Check Request.Files array in controller. 检查控制器中的Request.Files数组。 It should contain all your posted files from client 它应包含您从客户端发布的所有文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM