实体框架5 code首先添加图像 [英] Entity Framework 5 Code first adding an image

查看:134
本文介绍了实体框架5 code首先添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写与mvc4和Entity Framework 5一个非常小的应用程序。

我要添加产品和店面和形象的产品。

我有一个模型

  [表(CatalogItem)]
公共类CatalogItemModel
{
    [键]
    公众诠释CatalogItemId {搞定;组; }    公共字符串描述{搞定;组; }    公共双价{搞定;组; }    公众诠释ProductCount {搞定;组; }    公共字符串大小{搞定;组; }    公共字符串SKU {搞定;组; }    [专栏(类型名=图像)]
    公众的byte []图片{搞定;组; }    [显示(名称=显示项目目录)]
    公共BOOL DisplayItem {搞定;组; }
}

我的控制器。这从来没有被击中。​​

  [HttpPost]
    公众的ActionResult创建(CatalogItemModel catalogitemmodel)
    {
        如果(ModelState.IsValid)
        {
            db.CatalogItemModels.Add(catalogitemmodel);
            db.SaveChanges();
            返回RedirectToAction(「指数」);
        }        返回查看(catalogitemmodel);
    }

我的观点形成

 <&字段集GT;
    <传奇> CatalogItemModel< /传说>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Description)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Description)
        @ Html.ValidationMessageFor(型号=> model.Description)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Price)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Price)
        @ Html.ValidationMessageFor(型号=> model.Price)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.ProductCount)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.ProductCount)
        @ Html.ValidationMessageFor(型号=> model.ProductCount)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Size)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Size)
        @ Html.ValidationMessageFor(型号=> model.Size)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Sku)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Sku)
        @ Html.ValidationMessageFor(型号=> model.Sku)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.DisplayItem)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.DisplayItem)
        @ Html.ValidationMessageFor(型号=> model.DisplayItem)
    < / DIV>
    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(M = GT; m.Image)
    < / DIV>    <输入名称=图像类型=文件/>
    &所述p为H.;
        <输入类型=提交值=创建/>
    &所述; / P>
< /字段集>

当我尝试我的文件输入内发布一个新的目录与图像然而,它抛出一个错误


  

的输入不是有效的Base-64串,因为它含有非基本64字符,两个以上的填充字符,或填充字符之间非法字符。



解决方案

尝试解决这样的:

1。替换

<输入名称=图像类型=文件/>
<输入名称=镜像文件类型=文件/>

2。在控制器:

  [HttpPost]
        公众的ActionResult创建(CatalogItemModel catalogitemmodel,HttpPostedFileBase镜像文件)
        {
            使用(VAR毫秒=新的MemoryStream())
            {
                ImageFile.InputStream.CopyTo(毫秒);
                catalogitemmodel.Image = ms.ToArray();
            }            如果(ModelState.IsValid)
            {
                db.CatalogItemModels.Add(catalogitemmodel);
                db.SaveChanges();
                返回RedirectToAction(「指数」);
            }            返回查看(catalogitemmodel);
        }

I am writing a very small application with mvc4 and entity framework 5.

I want to add a product and store and image for the product.

I have a model

  [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; }
}

My controller. This never gets hit.

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

        return View(catalogitemmodel);
    }

My views form

    <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>

When I try posting a new catalog with an image within my file input however it throws an error

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

解决方案

Try to fix like that:

1 . Replace

<input name="Image" type="file"/> with <input name="ImageFile" type="file"/>

2 . In controller:

        [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);
        }

这篇关于实体框架5 code首先添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆