从字节数组ASP.NET MVC图像 [英] ASP.NET MVC image from byte array

查看:88
本文介绍了从字节数组ASP.NET MVC图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个字节数组重新presenting我的形象在我的视图模型。我用下面的code显示它:

currently I have a byte array representing my Image in my ViewModel. I display it with the following code:

<img src="@String.Format("data:image/gif;base64,{0}", Convert.ToBase64String(Model.Image))" />

现在,我不希望有一个Base64字符串在我的源文件,而是一个图像的链接。这样的:

Now I don't want to have a Base64 String in my Source file, but rather a link to a image. Like:

<img src="Images/" + Model.Id"/>

这将返回一个图像。

which would return a Image.

我如何写这样的方法返回一个图像链接?

How do I write such a method to return a Image link?

推荐答案

您可以定义一个控制器动作,这将有助于该图像:

You could define a controller action that will serve the image:

public class ImagesController: Controller
{
    public ActionResult Index(int id)
    {
        byte[] imageData = ... go get your image data from the id
        return File(imageData, "image/png"); // Might need to adjust the content type based on your actual image type
    }
}

和在你看来只是 IMG 标签的的src 属性指向该控制器动作:

and in your view simply point the src property of the img tag to this controller action:

<img src="@Url.Action("Index", "Images", new { id = Model.Id })" />

这篇关于从字节数组ASP.NET MVC图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆