我应该如何从控制器动作c#asp.net-MVC-2返回图像? [英] How should I return an Image from a controller action c# asp.net-mvc-2?

查看:222
本文介绍了我应该如何从控制器动作c#asp.net-MVC-2返回图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从构建图像字节[] 象下面这样。

I am building images from a byte[] like below.

public FileContentResult GetEmployeeImage(int empId)
{
   MemoryStream ms = new MemoryStream(byteArray);
   Image returnImage = Image.FromStream(ms);
   return returnImage;//How should i return this image to be consumed by javascript.
}

我想通过一个控制器的操作方法到该图像返回到浏览器,这样它可以通过我的javascript code被消耗掉,并在浏览器中显示。我应该怎么做呢?

I want to return this image to the browser via a controller action method, so as it can be consumed by my javascript code and displayed in the browser. How should I do this?

推荐答案

您不需要创建图像对象;你只是想返回原始数据。结果
该浏览器将读取原始数据转换成图像。

You don't need to create an image object; you just want to return the raw data.
The browser will read the raw data into an image.

return File(byteArray, "image/png");

显然,你需要通过正确的内容类型,具体取决于图像格式是字节数组中

Obviously, you need to pass the correct content type, depending on what image format is in the byte array.

这篇关于我应该如何从控制器动作c#asp.net-MVC-2返回图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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