从详细信息页面模板数据库使用ASP.NET MVC显示byte []数组形象 [英] Display byte[] array image from database in Details page template using ASP.NET MVC
问题描述
当我尝试使用详细信息页面模板中显示一个byte []数组图片:
公共FileContentResult RenderPhoto(字节[]的照片)
{
// VAR阵列=(字节[])会话[照片];
//返回文件(数组,图像/ JPEG); 返回文件(照片,图像/ JPEG);
}&所述; IMG SRC =@ Url.Action(RenderPhoto,Model.Photo)/>
照片为null。
如果我存储在student.Photo会话:
//
// GET:/学生/详细资料/ 5公众的ViewResult详细信息(INT ID)
{
学生学生= db.Students.Find(ID); 会话[照片] = student.Photo; 返回查看(学生);
}
和试图显示图像检索从会话值(注释行以上),它的工作原理。
为什么我在第一种情况下获得一个空值?
在通过学生查看后的ViewResult详细信息(INT ID)
, Model.Photo
不成立该值了吗?
为什么我在第一种情况下获得一个空值?
块引用>您不能传递一个字节数组在
&LT服务器; IMG>
标记。在< IMG>
标签简单地发送一个GET请求到指定的资源。要做到这一点的正确方法是使用一个ID:< IMG SRC =@ Url.Action(RenderPhoto,新的{PHOTOID = Model.PhotoId})/>
和则:
公众的ActionResult RenderPhoto(INT PHOTOID)
{
字节[] =照片FetchPhotoFromDb(PHOTOID);
返回文件(照片,图像/ JPEG);
}When I try to display a byte[] array image inside a Details page template using:
public FileContentResult RenderPhoto(byte[] photo) { // var array = (byte[])Session["photo"]; // return File(array, "image/jpeg"); return File(photo, "image/jpeg"); } <img src="@Url.Action("RenderPhoto", Model.Photo)"/>
photo is null.
If I store student.Photo in Session:
// // GET: /Student/Details/5 public ViewResult Details(int id) { Student student = db.Students.Find(id); Session["photo"] = student.Photo; return View(student); }
and try to display the image retrieving the value from Session (commented lines above) it works.
Why I'm getting a null value in the first case?
After passing student to the View in
ViewResult Details(int id)
,Model.Photo
doesn't hold that value anymore?解决方案Why I'm getting a null value in the first case?
You cannot pass a byte array to the server in an
<img>
tag. The<img>
tag simply sends a GET request to the designated resource. The correct way to do this is to use an id:<img src="@Url.Action("RenderPhoto", new { photoId = Model.PhotoId })" />
and then:
public ActionResult RenderPhoto(int photoId) { byte[] photo = FetchPhotoFromDb(photoId); return File(photo, "image/jpeg"); }
这篇关于从详细信息页面模板数据库使用ASP.NET MVC显示byte []数组形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!