Toubi
Toubi

Reputation: 2509

cant show byte[] image in FF and Chrome?

I'm showing images from database which are stored as Varbinary(Max)

In Razor I am converting it to a string and sowing as a <img>. It is working in IE but not in FF and Chrome. I have tried my best but couldn't figure it out why it is. Please help.

Please see this generated html in IE (working) and (FF & Chrome both not working):

http://jsfiddle.net/MGY9Y/

Note: I'm generating this html from Asp.Net MVC Razor using below code:

<img src=@(!Model.Photos.Any() ? "/Content/Images/Cars/CarPlaceHolder.jpg" 
:Encoding.ASCII.GetString(Model.Photos.FirstOrDefault().Document))></img>

Upvotes: 0

Views: 1070

Answers (1)

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

I am also doing the same thing but with different approach.. Controller

   using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
            {
                Image = binaryReader.ReadBytes(Request.Files["files"].ContentLength);
            }

View

   if (Model.Logo != null)
      {
         string imageBase64 = Convert.ToBase64String(Model.Logo);
         string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
         <img src="@imageSrc" class="self-image" />
      }
  else 
      {
         <img src=YOUR IMAGE class="self-image" />
      }

It is working in both IE and chrome

Upvotes: 1

Related Questions