我正在尝试从我的数据库中显示二进制图像,我可以从数据库中检索二进制数据,但是在视图中,但图像没有显示。
我试图通过访问与此问题相关的大部分博客来解决此问题,但失败了。 有人可以帮帮我吗?
这是我的Model类:
public class HotelRoomImage : MyBase
{
public int Id { get; set;}
public byte[] Image1 { get; set; }
public byte[] Image2 { get; set; }
public byte[] Image3 { get; set; }
public int HotelRoomId { get; set; }
public bool SelectById()
{
Command = MyCommand("select id, image1, image2, image3, roomId from RoomImages where id = 1");
Command.Parameters.AddWithValue("@id", Id);
MyReader = ExecuteReader(Command);
while (MyReader.Read())
{
Id = Convert.ToInt32(MyReader["Id"]);
Image1 =(byte[])MyReader["image1"];
Image2 = (byte[])MyReader["image2"];
Image3 = (byte[])MyReader["image3"];
HotelRoomId = Convert.ToInt32(MyReader["roomId"]);
return true;
}
return false;
}
}
我的控制器操作:
public ActionResult Viewimages()
{
HotelRoomImage ri = new HotelRoomImage();
ri.SelectById();
return View(ri);
}
以下是我的行动的观点:
@model UploadImageMVCApp.Models.HotelRoomImage
<div class="form-group col-md-4">
@{
if (Model.Image1 != null)
{
var imageBase64 = Convert.ToBase64String(Model.Image1);
var imageSrc1 = string.Format("data:image/jpg;base64,{0}", imageBase64);
<img src="@imageSrc1" alt="" width="400" height="400" />
}
}
</div>
<div class="form-group col-md-4">
@{
if (Model.Image2 != null)
{
string imageBase64 = Convert.ToBase64String(Model.Image2);
string imageSrc2 = string.Format("data:image/jpg;base64,{0}", imageBase64);
<img src="@imageSrc2" width="400" height="400" />
}
}
</div>
<div class="form-group col-md-4">
@{
if (Model.Image3 != null)
{
string imageBase64 = Convert.ToBase64String(Model.Image3);
string imageSrc3 = string.Format("data:image/jpg;base64,{0}", imageBase64);
<img src="@imageSrc3" width="400" height="400" />
}
}
</div>
答案 0 :(得分:0)
模型类
public class HotelRoomImage : MyBase
{
public int Id { get; set;}
public byte[] Image1 { get; set; }
public byte[] Image2 { get; set; }
public byte[] Image3 { get; set; }
public int HotelRoomId { get; set; }
}
public class Model_Crud
{
// image data should be there in varbinary(max) datatype column
// fallowing method is to fetch the row with id=1.
public HotelRoomImage SelectById()
{
HotelRoomImage model= new HotelRoomImage();
Command = MyCommand("select id, image1, image2, image3, roomId from RoomImages where id = 1");
// in the above select query i am hard coding id value as '1'......
MyReader = ExecuteReader(Command);
while (MyReader.Read())
{
model.Id = Convert.ToInt32(MyReader["Id"]);
model.Image1 =(byte[])MyReader["image1"];
model.Image2 = (byte[])MyReader["image2"];
model.Image3 = (byte[])MyReader["image3"];
model.HotelRoomId = Convert.ToInt32(MyReader["roomId"]);
}
return model;
}
}
<强>控制器强>
Model_Crud crud = new Model_Crud();
public ActionResult Viewimages()
{
HotelRoomImage ri = new HotelRoomImage();
ri = crud.SelectById();
return View(ri);
}