简体   繁体   English

使用EF5存储模型图像并在MVC视图中检索的最佳方法

[英]Best way to store model image using EF5 and retrieve in MVC View

I am looking for the best way to store image data in a model and then retrieve it in a view. 我正在寻找将图像数据存储在模型中然后在视图中检索它的最佳方法。

At the moment I have: 目前我有:

public class Product
{
  public int Id { get; set; }
  public byte[] Image { get; set; }
}

I then have a controller 然后我有一个控制器

public class ProductController
{
  public ActionResult List()
  {
    return View(Repository.GetAll())
  }
  public FileContentResult GetImage(int id)
  {
    Product p = Repository.GetById(id);
    return new FileContentResult(p.Image, "image/jpeg");
  }
}

A view... 一个看法...

@model IEnumerable<Product>
@foreach(var product in Model)
{
  <img src="@Url.Action("GetImage", "Person", new { id = item.Id })" alt="Person Image" />
}

What happens is as I get more and more products in the list, the images start to flash up on the screen as appose to loading instantly. 发生的事情就是当我在列表中获得越来越多的产品时,图像开始在屏幕上闪烁,因为它可以立即加载。

I am publishing to Azure so I can't really just go change byte[] to string and make string look in ~/Images/ProductImage.jpeg as I will have to publish the website every time I want to add a new product (as far as I am aware). 我正在发布到Azure所以我不能真的只是将byte[]更改为string并在~/Images/ProductImage.jpeg查找string ,因为每次我想要添加新产品时都必须发布网站(如据我所知)。

I am just looking for an alternative method of doing this or a reason why the images on my view appear gradually and flash up one after the other instead of instantly? 我只是在寻找另一种方法来做这个或者为什么我的视图上的图像逐渐出现并且一个接一个地闪现而不是立即闪现的原因?

The images "flash" up instead of instantaneously because your browser has to return to the server for each image. 图像“闪烁”而不是即时,因为您的浏览器必须返回到每个图像的服务器。 Many browsers have a limit on how many extra links (css files, js files, image files, etc.) that it can have open at any given time. 许多浏览器都限制了在任何给定时间可以打开多少额外链接(css文件,js文件,图像文件等)。 So that's why your images don't appear instantaneously. 这就是为什么你的图像不会立即出现的原因。

Some things you can do to help: 您可以做的一些事情来帮助:

  • If your images don't change often, add an [OutputCache(...)] attribute to your GetImage action. 如果您的图像不经常更改,请在GetImage操作中添加[OutputCache(...)]属性。 This way, the images will cache on the server and/or on the client for faster loading. 这样,图像将缓存在服务器和/或客户端上以加快加载速度。
  • Store your files locally on the server instead of in a database. 将文件本地存储在服务器上而不是数据库中。 This will let your IIS access the files directly as static files and avoid the MVC pipeline. 这将使您的IIS直接以静态文件的形式访问文件,并避免使用MVC管道。
  • Keep your files in the database, but cache them on your server (for example, under App_Data) as you retrieve them from the database. 将文件保留在数据库中,但在从数据库中检索文件时将它们缓存在服务器上(例如,在App_Data下)。 This avoids a database access. 这样可以避免数据库访问。

Edit: 编辑:

  • Store the image files in a publicly accessible (or semi-publicly) Azure blob. 将图像文件存储在可公开访问(或半公开)的Azure blob中。 Your img src tags would point directly to the blob for access. 你的img src标签会直接指向blob进行访问。

One of way of doing this would be to display the image directly from the byte array: 其中一种方法是直接从字节数组中显示图像:

<img src="data:image/png; base64, @Convert.ToBase64String(product.Image)">

Should work for small images. 应该适用于小图像。 You may also wish to store the image MIME so you can differentiate between data/png and data/jpg etc. 您可能还希望存储图像MIME,以便区分data / png和data / jpg等。

This should load the images immediately, as they will be part of the model sent to the view. 这应该立即加载图像,因为它们将是发送到视图的模型的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM