Entity Framework 5 Code First 添加图片

8

我正在使用MVC4和Entity Framework 5编写一个非常小的应用程序。

我想添加一个产品,并为该产品存储图像。

我有一个模型。

  [Table("CatalogItem")]
public class CatalogItemModel
{
    [Key]
    public int CatalogItemId { get; set; }

    public string Description { get; set; }

    public double Price { get; set; }

    public int ProductCount { get; set; }

    public string Size { get; set; }

    public string Sku { get; set; }

    [Column(TypeName = "image")]


    public byte[] Image { get; set; }

    [Display(Name = "Display Catalog Item")]
    public bool DisplayItem { get; set; }
}

我的控制器。它从未被调用过。

 [HttpPost]
    public ActionResult Create(CatalogItemModel catalogitemmodel)
    {
        if (ModelState.IsValid)
        {
            db.CatalogItemModels.Add(catalogitemmodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(catalogitemmodel);
    }

我的观点表单

    <fieldset>
    <legend>CatalogItemModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Price)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Price)
        @Html.ValidationMessageFor(model => model.Price)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ProductCount)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProductCount)
        @Html.ValidationMessageFor(model => model.ProductCount)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Size)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Size)
        @Html.ValidationMessageFor(model => model.Size)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Sku)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Sku)
        @Html.ValidationMessageFor(model => model.Sku)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.DisplayItem)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DisplayItem)
        @Html.ValidationMessageFor(model => model.DisplayItem)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m=>m.Image)
    </div>

    <input name="Image" type="file"/>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

当我尝试在文件输入中添加图片并发布新目录时,会出现错误。

该输入不是有效的Base-64字符串,因为它包含一个非Base-64字符、超过两个填充字符或填充字符中存在非法字符。


你能添加分配图像的代码吗? - nick_w
@NickW 刚刚添加了控制器动作。我已经添加了断点,但它从未被触发。 - Diver Dan
catalogitemmodel 怎么样了?它是如何创建的?如果断点没有被触发,那么问题就出现在之前了。 - nick_w
抱歉,我正在尝试使用Code First,这样它就可以为我处理了。 - Diver Dan
我的意思是,在你将图像的字节数组分配给CatalogItemModel.Image时,代码是什么? - nick_w
我不太确定我应该怎么做。我应该使用不同的属性来代替byte[]吗? - Diver Dan
1个回答

9

尝试按照以下方式修复:

1. 将

<input name="Image" type="file"/> 替换为<input name="ImageFile" type="file"/>

2. 在控制器中:

        [HttpPost]
        public ActionResult Create(CatalogItemModel catalogitemmodel, HttpPostedFileBase ImageFile)
        {
            using (var ms = new MemoryStream())
            {
                ImageFile.InputStream.CopyTo(ms);
                catalogitemmodel.Image =  ms.ToArray();
            }

            if (ModelState.IsValid)
            {
                db.CatalogItemModels.Add(catalogitemmodel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(catalogitemmodel);
        }

谢谢@testCoder,但是ImageFile始终返回null。我已经检查了拼写是否正确,但是没有成功。 - Diver Dan
使用 var files = Request.Files,我能够获取图像。 - Diver Dan
3
DiverDan 别忘了这个:@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))。它表示在使用 HTML 表单提交数据时,设置表单的编码类型为支持文件上传的多部分格式。 - testCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接