实体框架 – 如何使用MVC将图像保存到数据库4

前端之家收集整理的这篇文章主要介绍了实体框架 – 如何使用MVC将图像保存到数据库4前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个项目是购物车,我必须将图像保存到数据库,而不是将其上传到服务器,这里是我的模型
namespace ShoppingCart.Models
{
    [Bind(Exclude = "ItemID")]
    public class Item
    {
        [ScaffoldColumn(false)]
        public int ItemID { get; set; }
        [DisplayName("Category")]
        public int CategoryID { get; set; }
        [DisplayName("Brand")]
        public int BrandID { get; set; }
        [required(ErrorMessage = "A Name is required")]
        [StringLength(160)]
        public string Title { get; set; }
        public string Description { get; set; }
        [required(ErrorMessage = "Price is required")]
        [Range(0.01,100.00,ErrorMessage = "Price must be between 0.01 and 500.00")]
        public decimal Price { get; set; }
        [DisplayName("Album Art URL")]
        [StringLength(1024)]

        public string ItemArtUrl { get; set; }
        public byte[] Picture { get; set; }

        public virtual Category Category { get; set; }
        public virtual Brand Brand { get; set; }
        public virtual List<OrderDetail> OrderDetails { get; set; }
    }
}

所以我不知道如何去控制器插入图像或视图来显示它们,我已经搜索关于这个的信息,但我不能真正找到任何东西,我使用实体框架代码

解决方法

图像有两种简单的方法:一种是简单地将图像本身返回到控制器中:
[HttpGet]
    [AllowAnonymous]
    public ActionResult ViewImage(int id)
    {
        var item = _shoppingCartRepository.GetItem(id);
        byte[] buffer = item.Picture;
        return File(buffer,"image/jpg",string.Format("{0}.jpg",id));
    }

而这个观点只是引用它:

<img src="Home/ViewImage/10" />

此外,您可以将其包含在viewmodel中:

viewmodel.ImageToShow = Convert.ToBase64String(item.Picture);

并认为:

@Html.Raw("<img src=\"data:image/jpeg;base64," + viewmodel.ImageToShow + "\" />");

对于数据存储,您只需使用字节数组(varbinary(max))或blob或任何兼容类型。

上传图片

这里,一个名为HeaderImage的对象是EntityFramework EntityObject。控制器看起来像:

[HttpPost]
    public ActionResult UploadImages(HttpPostedFileBase[] uploadImages)
    {
        if (uploadImages.Count() <= 1)
        {
            return RedirectToAction("BrowseImages");
        }

        foreach (var image in uploadImages)
        {
            if (image.ContentLength > 0)
            {
                byte[] imageData = null;
                using (var binaryReader = new BinaryReader(image.InputStream))
                {
                    imageData = binaryReader.ReadBytes(image.ContentLength);
                }
                var headerImage = new HeaderImage
                {
                    ImageData = imageData,ImageName = image.FileName,IsActive = true
                };
                imageRepository.AddHeaderImage(headerImage);
            }
        }
        return RedirectToAction("BrowseImages");
    }

视图看起来像:

@using (Html.BeginForm("UploadImages","Home",FormMethod.Post,new { enctype = "multipart/form-data" }))
            {
                <div class="row">
                    <span class="span4">
                        <input type="file" name="uploadImages" multiple="multiple" class="input-files"/>
                    </span>
                    <span class="span2">
                        <input type="submit" name="button" value="Upload" class="btn btn-upload" />
                    </span>
                </div>
            }

猜你在找的MsSQL相关文章