c# - How to display database image (bytes[]) in mvc WebGrid -
i'm trying display byte[] image database in .net mvc webgrid column. i`m doing following steps
(ef model - articulos)
[key] public int id_art { get; set; } public int id_pro { get; set; } [stringlength(1)] public string tipo_pro { get; set; } [required] [stringlength(50)] public string nombre_pro { get; set; } [required] [stringlength(100)] public string descripcion_pro { get; set; } public byte[] imagen_pro { get; set; } [required] public decimal? precio_pro { get; set; } public int? estatus_pro { get; set; }
(controller)
public list<articulos> cargararticulos(string search, string sort, string sortdir, int skip, int pagesize, out int totalrecord) { using (dbmodel db = new dbmodel()) { var v = (from in db.articulos a.tipo_pro.contains(search) || a.nombre_pro.contains(search) || a.descripcion_pro.contains(search) select ); totalrecord = v.count(); v = v.orderby(sort + " " + sortdir); if (pagesize > 0) { v = v.skip(skip).take(pagesize); } return v.tolist(); } }
(view)
<div> @grid.table( tablestyle: "table table-responsive table-bordered", headerstyle: "webgrid-header", footerstyle: "webgrid-footer", alternatingrowstyle: "webgrid-alternating-row", rowstyle: "webgrid-row-style", columns: grid.columns( grid.column(columnname: "imagen_pro", header: "imagen", format: ), grid.column(columnname: "tipo_pro", header: "tipo"), grid.column(columnname: "nombre_pro", header: "nombre"), grid.column(columnname: "descripcion_pro", header: "descripcion"), grid.column(columnname: "precio_pro", header: "precio"), grid.column(header: "⇨", format: (item) => html.actionlink("ver", "ver", new { })) ) )
everything else works fine, i'm succesfully retrieving list controller, , displays webgrid, problem display byte[] image stored in database, don´t know how convert base64 before display in webgrid or adding righ format in column. i've been working on hours, please help.
grid.column(columnname: "imagen_pro", header: "imagen", format: ),
here's approach converting byte array base64 , embedding base64 string in img tag in grid. should able add static method controller this:
public static string convertbytearraytobase64(int id) { using (var db = new dbmodel()) { return convert.tobase64string(db.articulos.where(c => c.id_art == id).first().imagen_pro); } }
then add column this:
columns: grid.columns ( grid.column(header: "imagen", format: @<img src="data:image/jpeg;base64,@yourcontroller.convertbytearraytobase64(@item.id_art)" alt="">)) )
if it's png or gif, replace image/jpeg image/png or image/gif respectively.
Comments
Post a Comment