如何将控制器方法(id)中的字节数组image/png显示到Asp.net视图中


public ActionResult Overview(int subId, string countrycod)
{
    byte[] objByteArray =(byte[]) objResponse.Data;
    //return View();
    System.IO.MemoryStream imagestream = new System.IO.MemoryStream();
    imagestream.Write(objByteArray, 0, objByteArray.Length);
    //return new FileStreamResult(new System.IO.MemoryStream(objByteArray,true),"image/png");
    return new FileStreamResult(imagestream, "image/png");
}

您不需要内存流,可以直接返回字节数组:

public ActionResult Overview(int subId, string countrycod) 
{
    byte[] objByteArray = (byte[])objResponse.Data;
    return File(objByteArray, "image/png");
}

然后在视图中使用<img>标记,指向Overview控制器操作:

剃刀:

<img src="@Url.Action("Overview", "SomeController", new { subId = 123, countrycod = "en-US" })" />

WebForms:

剃刀:

<img src="<%= Url.Action("Overview", "SomeController", new { subId = 123, countrycod = "en-US" }) %>" />

最新更新