本文介绍了如何使用MVC 4将图像保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个项目,该项目是一个购物车,我要救,而不是把它们上传到服务器上的图像到数据库中,这里是我的模型

So I have a project which is a Shopping Cart, I have to save images to the database instead of uploading them to the server, here is my model

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

因此​​,进出口不清楚如何着手控制器插入图片或显示他们的观点,我有搜索这方面的信息,但我真的不能找到任何东西,使用即时通讯的实体框架code第一。

So Im unsure how to go about the controller to insert images or the view to display them, I have search for information about this but I cant really find anything, Im using entity framework code first.

推荐答案

有两个简单的方法可以做到图像 - 一个是简单地恢复图像本身的控制器:

There are two easy ways to do images -- one is to simply return the image itself in the controller:

    [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" />

此外,可以将其包含在视图模型:

Additionally, you can include it in the ViewModel:

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

和视图:

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

有关的数据存储,您只需使用一个字节数组( VARBINARY(最大))或BLOB或任何兼容类型。

For the data-store, you would simply use a byte array (varbinary(max)) or blob or any compatible type.

上传图片

下面,叫做对象标题图片是的EntityFramework EntityObject。该控制器看起来是这样的:

Here, an object called HeaderImage is an EntityFramework EntityObject. The controller would look something like:

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

视图看起来是这样的:

The View would look something like:

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

这篇关于如何使用MVC 4将图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:34