【发布时间】:2011-03-30 06:16:04
【问题描述】:
我目前遇到了使用 HtmlHelper 类显示图像的问题。
这就是我所拥有的。
我有一个应该显示图像的自定义 HtmlHelper 类:
public static string Images(this HtmlHelper helper, ......){
var writer = new HtmlTextWriter(new StringWriter());
byte[] bytearray = ... // some image byte array retrieved from an object.
// begin html image tag - this is where the problem is
writer.AddAttribute(HtmlTextWriterAttribute.Src, url.Action("GetPhoto", "Clinical", new { image = bytearray }));
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
// end of image tag
return writer.InnerWriter.ToString();
}
所以我上面尝试做的是将 Url.Action 注入到 img 源属性中。
我有一个控制器“GetPhoto”,它应该处理那个字节数组并返回一个图像。
public FileContentResult GetPhoto(byte[] image)
{
return File(image, "image/jpeg");
}
我设法到达控制器,但图像显示为空。有办法解决吗?或者可能有更好的方法来做到这一点?非常感谢您的帮助,谢谢!
【问题讨论】:
-
如果您能提供更多关于您打算如何使用它的信息,我认为这会有所帮助。传递图像的内容以在动作中渲染对我来说似乎没有必要。
标签: c# asp.net-mvc