【问题标题】:I can't get the value of my file input asp.net mvc Entity Framework [duplicate]我无法获得我的文件输入 asp.net mvc Entity Framework 的值 [重复]
【发布时间】:2016-04-18 06:57:19
【问题描述】:

我正在尝试将我的图像上传到一个文件夹中,并且我正在尝试将图像路径放入我的数据库中。但我无法在 asp.net mvc 中获取我的输入文件。 我该如何解决这个问题以及如何在我的 Controller 中调用输入文件?

创建.cshtml

@model SecundaireSchool.Models.tblLeraren

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm(new { enctype= "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>tblLeraren</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.naam, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.naam, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.naam, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.emailadres, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.emailadres, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.emailadres, "", new { @class = "text-danger" })
        </div>
    </div>

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

    <!-- <div class="form-group">
        @@Html.LabelFor(model => model.foto, htmlAttributes: new { @@class = "control-label col-md-2" })
        <div class="col-md-10">
            @@Html.EditorFor(model => model.foto, new { htmlAttributes = new { @@class = "form-control" } })
            @@Html.ValidationMessageFor(model => model.foto, "", new { @@class = "text-danger" })
        </div>
    </div> -->

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

<div>
@Html.ActionLink("Back to List", "Index")
</div>

教师控制器

所以我成功地将图像路径插入到我的数据库中。但它不会将它添加到我的图像文件夹中。我做错了什么?

[HttpPost]
    public ActionResult Create(tblLeraren teacher, HttpPostedFileBase file = null)
    {
        if (ModelState.IsValid)
        {

            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                file.SaveAs(path);
                teacher.foto = path;
            }

            db.tblLerarens.Add(teacher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(teacher);
    }

【问题讨论】:

  • 您没有带有name="file" 的文件输入(但您确实有带有name="foto" 的文件输入-检查您生成的html!)您不能使用new { @name="file" } (fortunately). Create a separate input for the file upload - 更改name 属性 提交时会绑定到你的参数。

标签: asp.net asp.net-mvc


【解决方案1】:

在 MVC 中,文件对象作为 HttpPostedFileBase 的对象返回。因此,只需在您发布表单的控制器中捕获此对象,它就会起作用。

[HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file)
    {

    }

【讨论】:

    【解决方案2】:

    您的表单需要是多部分的才能发布图片。这是一个如何将图像发布到控制器操作方法的示例。

    @using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div class="form-group">
            <div style="position:relative">
                    <input type="file" name="Image" size="40" />
            </div>
        </div>
    
        <button type="submit">Save</button>
    }
    

    在您的 AccountController 中有此“编辑”操作方法并归因于 HttpPost

    [HttpPost]
    public ActionResult Edit(HttpPostedFileBase image = null)
    {
                 //Do what ever your want with image
    }
    

    您的图像现在应该具有张贴图像的值。

    【讨论】:

      【解决方案3】:

      第一: 将以下属性添加到表单:

      enctype = "multipart/form-data" 
      

      像这样:

      @using (Html.BeginForm("Ceate", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
      {
      }
      

      并在您的操作中添加HttpPost 像这样:

          [HttpPost]
      Public ActionResult Create(httpPostedFileBase file){}
      

      您的输入文件和参数名称应该相同:

      <input type="file" name="file"/>
      

      注意:如果你的输入文件和参数名称不一样,binder enjein 不能绑定它,你应该得到这样的文件

          foreach (string file in Request.Files)
      {
         HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
         if (hpf.ContentLength == 0)
            continue;
         string savedFileName = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, 
            Path.GetFileName(hpf.FileName));
         hpf.SaveAs(savedFileName);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 2016-07-02
        • 1970-01-01
        • 2012-04-17
        • 2022-08-05
        • 1970-01-01
        相关资源
        最近更新 更多