andrewlocatelliwoodcock

Thoughts on Software

Displaying a Base64-encoded string as an image in a view

with 2 comments


Following on from my previous posts covering the server-side code to display a base64-encoded string as an image in MVC, I thought it would be useful to show how to write a View to actually display the re-hydrated image. What hoops will we have to jump through, what arcane secrets must be learnt?

To MVC’s credit, once again the answer is: none. It couldn’t actually be simpler:

@{
    View.Title = @View.Title;
}

<h2>@View.Message</h2>

<img src="/Images/TestImage" height="100px" width="80px" />

There you go: you actually only need specify the src attribute of the image tag to be the Controller/Action of the method that returns the re-hydrated image.

Note that in this example we’re not even specifying the image name or format.


Written by andrewlocatelliwoodcock

April 30, 2011 at 11:20

2 Responses

Subscribe to comments with RSS.

  1. […] a string from a call to an Action method instead of a formal View; perhaps you want to return a Base64 encoded image or display the contents of a file, for example. ASP.Net MVC actually makes this very […]

  2. If your image is stored as a byte stream, you can also point it directly at the src attribute as a byte stream. If I am not mistake

    downeyk

    June 6, 2013 at 15:08


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: