首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpPostedFileBase到可变二进制(最大值)

HttpPostedFileBase到可变二进制(最大值)
EN

Stack Overflow用户
提问于 2016-02-29 02:09:41
回答 2查看 1.5K关注 0票数 0

我刚刚尝试了很多东西,但最终都没有成功。

首先,我有下一段代码,它只是做一些事情,但不保存图像。

如何将图片保存到varbinarymax中?接下来如何将它们显示在视图中?

查看:

代码语言:javascript
复制
<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>

控制器:

代码语言:javascript
复制
[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);
}

型号:

代码语言:javascript
复制
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; }
}
EN

回答 2

Stack Overflow用户

发布于 2016-02-29 03:45:34

将View First更改为:

代码语言:javascript
复制
@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>
}

将操作更改为以下内容:

代码语言:javascript
复制
[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);
    }
}

现在这个徽标保存它。

票数 1
EN

Stack Overflow用户

发布于 2016-02-29 02:49:33

由于您还没有张贴完整的表单,这里是上传图像并保存到数据库的完整代码。

窗体必须具有enctype属性。

代码语言:javascript
复制
@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" />
}

在你的行动中。

代码语言:javascript
复制
[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);
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35686288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档