c# - 如何从 Controller 方法返回多个图像?

标签 c# asp.net-mvc image-processing

我有一个生成图像的 ASP.NET MVC Controller (它们存储在内存中,我不想将它们存储在硬盘上)并且应该将它们返回到我的 View 中。

问题是:我不知道如何从一个 Controller 方法返回多个图像(我想在 1 个 View 中显示图像)。我知道我可以使用 FileResult 返回单个图像例如,但我不知道(不在 google/stackoverflow 上)如何从同一方法返回多个图像。无法将方法拆分为多个方法。哦,所有图像都转换为byte[] ,但如有必要,可以逆转。

最佳答案

如果您有权访问图像的 MIME 类型,则始终可以将它们呈现为 Base64 编码的图像,而不是向不同的 Controller 方法发出另一个请求。这是我使用的 View 模型:

public class ImageViewModel
{
    public string FileName { get; set; }

    public string MIME { get; set; }

    public byte[] Data { get; set; }

    public override string ToString()
    {
        return string.Format(@"data:{0};base64,{1}", MIME.ToLower(), Convert.ToBase64String(Data));
    }
}

您可以使用 Filename alt 中的属性(property)<img /> 的属性标记,因此您 View 中的标记和模型绑定(bind)看起来像这样(假设 Razor 语法):
<img src="@model.ToString()" alt="@model.FileName" />

你确实会丢失图像缓存,AFAIK - 这对我来说不是问题,但对某些人来说是可以理解的。

关于c# - 如何从 Controller 方法返回多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14844237/

相关文章:

python - 如何在 PIL 图像中单独旋转字母?

c - 如何使用android ndk在android中旋转图像

c# - SQL查询中如何设置表名

C# - 迭代多个数组/列表的最有效方法

c# - 基于 Accept header 的 ASP.NET Core Web API 操作选择

jquery - 如何将多个参数从ajax传递到mvc Controller ?

c# - MiniProfiler 类型存在于 Miniprofiler.Shared 和 MiniProfiler 中

asp.net-mvc - ASP.Net MVC : What’s the Difference Between a Value Provider and Model Binder

c# - 在 Html 元素的深层堆栈中获取一个元素

image - 如何从图像中提取图层(jpg,png等)