【问题标题】:ASP.NET -Retrieving a list of images from the Database and display them in the view?ASP.NET - 从数据库中检索图像列表并将它们显示在视图中?
【发布时间】:2013-04-18 12:04:46
【问题描述】:

我想从数据库中检索图像列表并将它们显示在视图上。所有图像都以“图像”数据类型格式存储在数据库中。下面是我的控制器、视图模型和视图。我不确定它是否正确,所以请帮忙。我刚刚使用了 Linq2SQL,因为它只是一个示例项目。请更正我的代码,并帮助我如何在视图上显示图像列表。我只是一个初学者,所以如果你能保持解决方案简单明了,那就太好了。

视图模型:

public class ImageViewModel
{
    public byte[] Image { get; set; }
    public int imageId { get; set; }
    public IEnumerable<int> imageList { get; set; }     
}

控制器: *(已更新并已解决)*

   public ActionResult Index()
{

    var imagesList = (from q in ctx.Images
                      select new Models.ImageViewModel
                      {
                      imageId = q.id
                      }).ToList();

    IEnumerable<int> imageIds = imagesList.Select(x=>x.imageId);
    Models.Test obj = new Models.Test();
    obj.imageList = imageIds;
    return View(obj);
}

    public ActionResult View(int imageId)
    {
        byte[] imageArray = (from q in ctx.Images
                             where q.id == imageId
                             select q.data.ToArray()).FirstOrDefault(); 
        return File (imageArray,"image/jpg");
    }

查看:

   @model MvcApplication3.Models.ImageViewModel

   @foreach (var id in Model.imageList)
{ 
    <img src="@Url.Action("View", "Home", new { imageId = id},null)" />
}

【问题讨论】:

  • 您遇到了什么问题?
  • 我根本看不到任何图像。只是一个带有“X”标记的小方框列表。
  • 我也认为我的逻辑可能是错误的。请随时为我提供执行此操作的替代解决方案。谢谢
  • 您的视图模型中没有名为bitimage 的属性,但您在视图中引用了它。您是否有下载图像本身的处理程序或操作?请记住,imgsrc 属性需要图像的 URL,而不是图像本身。
  • 遵循 Britton 的以下建议 - 您需要构建一个动作来下载每个图像。

标签: c# .net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

创建一个新操作,为您的图像获取一个唯一标识符,使用该 ID 从您的数据库中获取 byte[],然后将其直接写入响应流,如下所示

Response.ContentType = "content type of your image";
//filecontents is the byte[]
Response.BinaryWrite(filecontents);

然后,当您需要显示数据库中的图像时,您可以像这样设置源

<img src="/Controller/Action/ImageId"/>

【讨论】:

  • 谢谢,但我不会尝试发送请求以检索特定图像。我只想从数据库中抓取一堆图像并将其显示给用户。类似于在员工表中显示所有员工的列表。你能再看看我的控制器和视图模型,看看有什么问题吗?
  • 一旦您执行此操作来检索特定图像,您所需要的只是图像 ID 列表。然后在您的视图中循环遍历 ID 列表并像我上面那样构建图像标签。
  • 酷。现在试一试。谢谢
【解决方案2】:

布里顿也推荐了这个,但我会重申。

你应该有两个动作/视图来实现你想要的。一个用于检索单个图像,另一个是使用前面提到的操作检索单个图像的所有图像列表。

这是我的意思的一个非常粗略的例子。我还没有对此进行测试以确保它可以编译,但它应该让您大致了解如何执行此操作。

class ImageLibrary
}
    public ActionResult List()
    {
        //I'm going to assume here you have some sort of service or repository for obtaining your images at this point.
        //This doesn't retrieve the actual image data, just the metadata. It's less of a hit on the DB this way
        var imagesList = imageService.ListAllImages()
        IEnumerable<int> imageIds = imagesList.Select(x=>x.ImageId)
        return View("List", imageIds)
    }

    public ActionResult View(int imageId)
    {
        var image = imageService.GetImage(imageId);

        var contentDisposition = new System.Net.Mime.ContentDisposition() { FileName = image.Name, Inline = true };
        Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
        return File(image.Data, image.ContentType); //including content type since I'm assuming the image could be a jpg, gif, png, etc./
    }

}

ImageLibrary/List 视图中。

@foreach(var id in Model)
{
    <img src="@Url.Action("View", "ImageLibrary", new {imageId = id})">
}

【讨论】:

  • 谢谢。现在更有意义了。
  • 您能解释一下 imageService 的作用吗?就像我提到的,我是 MVC 的初学者,我认为这对我来说有点太高级了。我知道如何通过将数据映射到视图模型(如在我的控制器中编写的那样)来访问数据,但还没有开始使用存储库。我已经完成了第一个动作,到目前为止看起来还不错。
【解决方案3】:

我不确定您是否将数据作为 base64 字符串获取,如果是,您可以尝试:

<img src="data:image/png;base64,@(item.bitimage)" alt="test.png" />

尝试失败:

<img src="data:image/png;base64,@ToBase64String(item.bitimage)" alt="test.png" />

http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/

【讨论】:

  • 感谢您的回复。我已经尝试了您的代码,但仍然没有运气。你能看看我的控制器和视图模型,看看那里有没有问题?
  • 如果使用 item.image 会怎样?
  • 请注意,除了最小的图像之外,这对任何东西都是一种糟糕的方法,因为它会减慢页面加载速度并停止浏览器缓存任何图像。
  • 确实,它确实将所有负载都放在了一个请求上,我可以想象这会如何增加,我建议使用 Britton 的方法。
  • 由于代表级别,我无法评论他的方法,只需创建控制器并在您的列表视图中显示一个带有每个实例 id 的图像标签。
猜你喜欢
  • 2013-01-24
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
相关资源
最近更新 更多