我应该如何从控制器动作c#asp.net-mvc-2返回一个图像?

sha*_*e87 5 javascript c# image actionmethod asp.net-mvc-2

我正在构建如下图像byte[].

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.
}
Run Code Online (Sandbox Code Playgroud)

我想通过控制器操作方法将此图像返回到浏览器,因此它可以被我的javascript代码使用并显示在浏览器中.我该怎么做?

SLa*_*aks 8

您不需要创建图像对象; 你只想返回原始数据.
浏览器会将原始数据读入图像.

return File(byteArray, "image/png");
Run Code Online (Sandbox Code Playgroud)

显然,您需要传递正确的内容类型,具体取决于字节数组中的图像格式.

  • 你的意思是: return new FileContentResult(byteArray, "image/png"); (2认同)