我是ASP.NET MVC的新手,所以请不要评判我……我遇到了一个问题,SQL Server中的图像(字节数据类型(没有显示在我的视图中。上面写着";无法将byte[]
转换为字符串";。我该怎么办?
这是我的控制器方法ViewProduct
:
public ActionResult ViewProduct()
{
return View();
}
public ActionResult ViewProd()
{
inventoryDBEntities1 dbe = new inventoryDBEntities1();
return View(dbe.tbl_product.ToList());
}
这是我的模型类tbl_product
:
public partial class tbl_product
{
public int productID { get; set; }
public byte[] prod_image { get; set; }
}
这是我的观点:
@model IEnumerable<PointofSale.Models.tbl_product>
<table>
<tr>
<td>
Image
</td>
</tr>
<tr>
@foreach (var item in @Model)
{
<td>
// The error is around here ( V )!!
<img src="@Url.Content(item.prod_image)" height="100" width="100"/>
</td>
}
</tr>
</table>
您需要用byte[]
显示图像,方法是将其转换为base64字符串。
@foreach (var item in @Model)
{
<td>
@{
string imageBase64Data = Convert.ToBase64String(item.prod_image);
string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data);
<img src="@Url.Content(imageDataURL)" height="100" width="100" />
}
</td>
}
参考文献
在ASP.NET MVC 中显示来自字节数组的图像