andrewlocatelliwoodcock

Thoughts on Software

Decoding a Base64-encoded string into a JPG

leave a comment »


In a previous post, I discussed using MVC’s FileResult action return type to return an Image re-hydrated from a Base64-encoded string. So how exactly do you go from a Base64-encoded string to a FileResult object?

There are several, fairly straight forward steps:

  1. Convert the base64-encoded string to a byte array
  2. Use the byte array to instantiate a new object of type FileContentResult, which inherits from FileResult.
    FileContentResult expects two parameters in its constructor: a byte array representing the contents of the file, and a string representing the file format.

Some sample code to re-hydrate a jpg image from its base64-encoded representation:

    var imageBytes = Convert.FromBase64String(base64String);
    var image = new FileContentResult(imageBytes, "jpg");

Written by andrewlocatelliwoodcock

April 27, 2011 at 21:00

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: