0

我刚刚尝试了很多我发现的东西,但最后都没有成功。

首先,我有下一个代码,它只是做的事情很好,但不保存图像。

我应该怎么做才能将图像保存到 varbinarymax 中?以及接下来如何将它们展示给视图?

看法:

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

控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school)
{
  if (ModelState.IsValid)
  {
    db.School.Add(school);
    await db.SaveChangesAsync();
    return RedirectToAction("Index");
  }
  return View(school);
}

模型:

public partial class School
{
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  public School()
  {
    this.Product = new HashSet<Product>();
  }

  public int Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string Description { get; set; }
  public string Mail { get; set; }
  public int? Phone { get; set; }
  public byte[] Logo { get; set; }
  public string Small_Description { get; set; }

  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  public virtual ICollection<Product> Product { get; set; }
}
4

2 回答 2

1

首先将视图更改为:

@using (Html.BeginForm("Create", "Schole", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
  @Html.LabelFor(model => model.Logo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Logo, new { type = "file" })
   <input type="submit" value="submit" />
</div>
</div>
}

将操作更改为:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school, HttpPostedFileBase Logo)
    {
        if (ModelState.IsValid)
        {
            using (var memoryStream = new MemoryStream())
            {
                Logo.InputStream.CopyTo(memoryStream);
                school.Logo = memoryStream.ToArray();
            }
            db.School.Add(school);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(school);
    }
}

现在这个标志保存它。

于 2016-02-28T19:45:33.573 回答
0

由于您没有发布完整的表格,这里是上传图像并保存到数据库的完整代码。

您的表单必须具有enctype属性。

@using (Html.BeginForm("Index","Home",FormMethod.Post, new{ enctype = "multipart/form-data" }))
{
    //your other code
    <input type="file" name="logo" />
    <input type="submit" value="Save" />
}

在你的行动中。

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school)
{
  if (ModelState.IsValid)
  {
        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(Request.Files["logo"].InputStream))
        {
            fileData = binaryReader.ReadBytes(Request.Files["logo"].ContentLength);
        }
    school.Logo=fileData;
    db.School.Add(school);
    await db.SaveChangesAsync();
    return RedirectToAction("Index");
  }
  return View(school);
}

它将文件保存在您的数据库中。

于 2016-02-28T18:49:33.373 回答