从数据库中提取图像 [英] rerieving image from database
问题描述
我试图恢复在Oracle数据库中存储为blob的图像。我正在使用MVC和
ViewData将图像从控制器获取到View。
-------------------模型代码在这里----------- ---
公共类SelectionModel(这是模型类)
{
public byte [] ImageDsiplay
{
get;
设定;
}
public MemoryStream ImageStream
{
get;
设定;
}
}
----------控制器代码IS这里-----
public ActionResult GetImageInfo(SelectionModel sel)(这是控制器方法)
{
byte [] imagedata;
string sql =从文件库中选择文件内容,其中rownum = 1;
OracleConnection con = new OracleConnection(ConStr);
OracleCommand com = new OracleCommand(sql,con);
尝试
{
con.Open();
OracleDataReader dr;
dr = com.ExecuteReader();
while(dr.Read())
{
imagedata =(byte [])dr [0];
sel.ImageDsiplay = imagedata;
var stream = new MemoryStream(sel.ImageDsiplay);
sel.ImageStream = stream;
}
}
catch(exception ex)
{
}
return View(sel);
}
-----这是从控制器获取图像的视图 - -------
< img src =@ ViewData [image],width =500pxalt =Alternate Text/>
现在我的问题是:我错过了什么吗?因为我无法在我的视图中看到图像。图像标签是否有问题?我可能会遗漏一些东西。我也尝试过:
SRC =DATA:IMAGEdata:image / gif; base64
任何解决方案?
< blockquote>你可以使用retrun文件来返回一个Image文件:
return 文件(sel.ImageStream , image / png);
并在您的视图中,您可以调用您的操作结果
< img style = width:260px src = @ Url.Action( ActionResult, Controller) alt = myimage / >
我希望它可以帮到你,
但关于你的代码,没有'你在控制器中分配了viewdata集合吗?如果你没有为它分配任何值,它就不起作用,而且我认为它只适用于简单的数据类型,而不适用于blob数据。
I am trying to rerieve image which is stored as blob in Oracle Database .I am using MVC and
ViewData to get the image from controller to View .
------------------- Model CODE IS HERE--------------
public class SelectionModel (This is the model class ) { public byte[] ImageDsiplay { get; set; } public MemoryStream ImageStream { get; set; } }
----------Controller Code IS HERE-----
public ActionResult GetImageInfo(SelectionModel sel) (This is the controller method) { byte[] imagedata ; string sql = "select filecontent from filestore where rownum=1 "; OracleConnection con = new OracleConnection(ConStr); OracleCommand com = new OracleCommand(sql, con); try { con.Open(); OracleDataReader dr; dr = com.ExecuteReader(); while (dr.Read()) { imagedata = (byte[])dr[0]; sel.ImageDsiplay = imagedata; var stream = new MemoryStream(sel.ImageDsiplay); sel.ImageStream = stream; } } catch (Exception ex) { } return View(sel); }
-----Here is the view which gets the image from the controller---------
<img src="@ViewData["image"]", width="500px" alt="Alternate Text" />
Now My question is: Am I missing anything? Because I can't see the image in my View. Is thre anything wrong with the image Tag? I might be missing something. I have also tried:
SRC="DATA:IMAGEdata:image/gif;base64
Any solution?
You can use retrun file, for returning an Image file:
return File(sel.ImageStream , "image/png");
and on your view you can call your action result
<img style="width:260px" src="@Url.Action("ActionResult", "Controller")" alt="myimage" />
I Hope it help you,
But about your code, didn't you assign viewdata collection in your controller?,it doesn't work if you don't assign any value to it, and also i think it is only for simple data types, and not for blob data.
这篇关于从数据库中提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!