Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net Mvc Load Image (img src) from Byte[] in javascript
    primarykey
    data
    text
    <p>I'm developing an application with ASP.Net Mvc4, Razor and javscript/Jquery. I need to display images that are stored on a remote server, these images will fall within a component "slider".</p> <p>I recover the address remote of the images and these store them in a List of byte[] from controller with json. The Class ByteImages:</p> <pre><code> public class ByteImagenes { public byte[] byimagen { get; set; } public ByteImagenes(byte[] byImagen) { byimagen = byImagen; } } </code></pre> <p>The Action Controller:</p> <pre><code> public JsonResult AlbumFoto(string Directorio){ var Lista = Galeria(true, Directorio); return Json(new { Fotos = Lista }); } private List&lt;ByteImagenes&gt; Galeria(bool Remoto, string Directorio){ string path = string.Empty; DirectoryInfo dir = null; List&lt;ByteImagenes&gt; flistImagenes = new List&lt;ByteImagenes&gt;(); if (!Remoto){ path = "/" + Directorio + "/"; dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + Directorio); } else { path = Directorio; dir = new DirectoryInfo(Directorio); } FileInfo[] fileList = dir.GetFiles("*.*", SearchOption.AllDirectories); var fileQuery = from file in fileList where ((file.Extension == ".jpg") || (file.Extension == ".png") || (file.Extension == ".gif") || (file.Extension == ".jpeg")) orderby file.Name select file; foreach (var file in fileQuery){ var b = System.IO.File.ReadAllBytes(path + file.Name); flistImagenes.Add(new ByteImagenes(b)); } return flistImagenes; } </code></pre> <p>In the View, I'm creating my gallery pictures in a function javascript. In this function javascript I can't show the images. function javascript:</p> <pre><code> function CrearGaleria(data) { var elem; $.each(data.Fotos, function (key, val) { var _ParamImp = val.byimagen; var link2 = '@Html.Raw("data:image;base64,@System.Convert.ToBase64String(Param)")'; link2 = link2.replace("Param", _ParamImp); alert(link2); elem = "&lt;li&gt;&lt;a&gt;&lt;img src=" + link2 + " /&gt;&lt;/a&gt;&lt;/li&gt;"; $("#ulImages").append(elem); }) iniciarEfecto(); } </code></pre> <p>the alert(link2)this show me: <img src="https://i.stack.imgur.com/KOpvn.png" alt="enter image description here"></p> <p>I can't load the image in src from array byte how I can resolve this problem?</p> <p><strong>EDIT</strong> I can resolve this problem... I change my class before byte[] now string</p> <pre><code>public class Imagenes { public string imagen { get; set; } public Imagenes(string sImagen) { imagen = sImagen; } } </code></pre> <p>and when load the values I change in private List Galeria in foreach Convert.ToBase64String</p> <pre><code>foreach (var file in fileQuery) { var b = System.IO.File.ReadAllBytes(path + file.Name); listImagenes.Add(new Imagenes(Convert.ToBase64String(b))); } return listImagenes; </code></pre> <p>and in javascript in the view:</p> <pre><code>function CrearGaleria(data) { var elem; $.each(data.Fotos, function (key, val) { elem = '&lt;li&gt;&lt;a&gt;&lt;img src="data:image;base64,' + val.imagen + '"/&gt;&lt;/a&gt;&lt;/li&gt;'; $("#ulImages").append(elem); }) iniciarEfecto(); } </code></pre> <p>Now I can show the images. :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload