Click here to Skip to main content
14,577,068 members
Rate this:
Please Sign up or sign in to vote.
See more:

I already have stored image into database using below code
user.UserImage = new Byte[image1.ContentLength];
                   image1.InputStream.Read(user.UserImage, 0, image1.ContentLength);

Now I want to Store this into session and Retrieve on Dashboard Page.

But image is not showing. pls help.

What I have tried:

In Controller:
string imageDataURL = string.Format("data:image/png;base64,{0}", dataitem.UserImage);
               Session["UserImage"] =  imageDataURL;

In View:

var img =  (Session["UserImage"]);
  string imageSrc = string.Format("data:image/gif;base64,{0}", img);
  <img src="@imageSrc" width = "60" height = "60" class="img-profile rounded-circle"/>
Updated 29-Jun-20 6:49am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The data you store in the Session already has the data:image/gif prefix, however you add it again in your view, so just miss that step out

    var img = (Session["UserImage"]);
<img src="@img" width="60" height="60" class="img-profile rounded-circle" />

Also you don't seem to be converting the byte array to base64, if dataitem.UserImage is a byte array then use

string imageDataURL = string.Format("data:image/png;base64,{0}", System.Convert.ToBase64String(dataitem.UserImage));
[no name] 29-Jun-20 13:05pm
Thanks a lot F-ES Sitecore

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100