问题描述
在视图中:
<img src="@Url.Action("GetPhoto","Productos",new { code=code})" alt="" class="img-responsive center-block" style="max-width:600px;max-height:200px" />
在控制器中:
public ActionResult GetPhoto(int code)
{
try
{
conexion.conectar();
string sql = "SELECT photo,tipo as extension FROM photos WHERE code=" + code;
byte[] imagen = null;
string extension = "";
string nombre = "photo";
using (MysqLCommand cmd = new MysqLCommand(sql,conexion.con))
{
using (MysqLDataReader d = cmd.ExecuteReader())
{
if (d.Read())
{
imagen = (byte[])d["photo"];
extension = (string)d["extension"];
nombre = nombre + code.ToString() + "." + extension;
}
else
{
var folder = Server.MapPath("~/Images/imagen_sin_foto.png");
string contentType = "image/png";
byte[] photo_null = System.IO.File.ReadAllBytes(folder);
return File(photo_null,contentType,"imagen.png");
}
}
}
conexion.cerrar();
//return File(imagen,"image/jpeg");
return File(imagen,"application/octet-stream",nombre);
}
catch (Exception ex)
{
return Json(ex);
}
}
但就我而言,我必须显示多张照片(通常约3张)。这个想法不是要多次调用GetFoto函数到数据库。想法是能够将其放入对象照片列表中:
public class PhotosModels
{
public string name{ get; set; }
public string code{ get; set; }
public byte[] photo{ get; set; }
}
并从控制器发送一个List<PhotosModels>
,因此在视图中可以使用foreach。这个想法是优化视觉图像的性能和负载。我该怎么办?一些例子吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)