segunda-feira, 13 de janeiro de 2014

Como renderizar imagens usando ajax

Código no servidor (c#):
public ActionResult Index(int idFoto)
{
 byte[] foto = fotoRepository.GetById(idFoto);
 return Content(Convert.ToBase64String(foto), MediaTypeNames.Image.Gif);
}
Código no cliente (javascript):
$.ajax({
    type: "GET",
    url: 'http://localhost/imagens/1',
    success: function (data) {
        $('#myImg').attr('src', 'data:image/gif;base64,' + data);
      },
    error: function(e) {
        alert('erro');
    }
 });

Nenhum comentário: