本文介绍了检索和使用ASP.Net MVC从数据库中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从数据库中检索图像,然后将其发送到查看和显示为缩略图。这里是我的code,但它有一些错误,我无法正常返回到图像显示。
我使用ASP.Net MVC和aspx页面。
控制器:
公众的ActionResult showImg(INT ID)
{
VAR为imageData =从db.Products米
其中,m.ShopId == 3
选择Image.FromStream(新的MemoryStream(m.Product_img.ToArray())); 返回新FileStreamResult(新System.IO.MemoryStream(为imageData),图像/ JPEG);
}
查看:
< IMG SRC ='<%= Url.Action(showImg,图像,新{ID =计算机[imageID]})%>' />
解决方案
看起来你并不需要重建图片
对象在LINQ的语句,只返回流,并通过它你的 FileStreamResult
:
VAR图像=(从米db.Products
其中,m.ShopId == 3
选择m.Product_img).FirstOrDefault();VAR流=新的MemoryStream(image.ToArray());返回新FileStreamResult(流,图像/ JPEG);
I want to retrieve an image from database and then send it to view and display it as thumbnail. Here is my code but It has some error and I can't return the image properly.I'm using ASP.Net MVC and aspx pages.
Controller :
public ActionResult showImg(int id)
{
var imageData = from m in db.Products
where m.ShopId == 3
select Image.FromStream(new MemoryStream(m.Product_img.ToArray()));
return new FileStreamResult(new System.IO.MemoryStream(imageData), "image/jpeg");
}
View :
<img src='<%= Url.Action("showImg", "image", new { id = ViewData["imageID"] } ) %>' />
解决方案
It looks like you do not need to recreate the Image
object in your linq statement, just return the stream and pass it your FileStreamResult
:
var image = (from m in db.Products
where m.ShopId == 3
select m.Product_img).FirstOrDefault();
var stream = new MemoryStream(image.ToArray());
return new FileStreamResult(stream, "image/jpeg");
这篇关于检索和使用ASP.Net MVC从数据库中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!