About Blobs and Accessing Media by code in EPiServer 7.5

In this post I take a look at what blob storage exactly is, why it could be interesting and how it works in EPiServer. By default EPiServer uses blob storage to store all its content. Further I take a look at how to access stored media assets from code and show them in a view and how to save an uploaded image as a blob in the Media panel.

BLOB (Binary Large Object) providers is a framework designed to store large amounts of binary data in a more optimized and cost-effective solution such as cloud storage, instead of in the database. The EPiServer platform supports BLOB storage of assets using a provider-based setup, and has a built-in file BLOB provider. By default this provider will store files on local disc or a file share which will be defined during installation. – EPiServer World

Advantages of blob storage are:

  • Scalability
  • Better performance
  • Assets can be locked from outside manipulation (an image stored as a file on your system can be modified by other applications then you application without you knowing it)

So how does it work in EPiServer?

Attaching an existing Media file to a ContentReference property in your (View)Model by code.

First lets say you want to attach an existing Media file from the Media panel to a ViewModel ContentReference property and display it. Take a look at the Media panel from the Alloy MVC Sample page.

EPiServer Media Panel

EPiServer Media Panel

Now ofcourse you can make a contentreference property on your startpage and let the editor attach this media file to that property using the CMS but what if we want to do this by code?

Dragging your mouse over an image shows its content ID

Dragging your mouse over an image shows its content ID

If you hover your mouse over the image you see its content ID which is 87 in this case.
Now add a simple property to your startpage type model (~/Models/Pages/StartPage.cs)

Next in your controller attach the image to this property:

And in your view lets display it:

@Url.ContentUrl can be used for all ContentReference, also PageReferences because they inherit from ContentReference to get a URL to that specific content.

 

How to loop through all available Media?

You could ask yourself now what if I dont know the ID and I dont want to find it first using the trick of going to the CMS, mouseover the item and check the ID. What if i want to do the searching for media also from code?

Looping through all assets is easy if you have a ContentReference. If you dont have one start from the GlobalAssetsRoot.

This will not loop through all the subfolders in the Media Panel. For that you need to use GetChildren<ContentFolder>.

By combining these two loops you can find Media with a specific filename located in any of the subfolders and get its ContentReference. How cool is that!

 

Creating a new Image and saving it in the Media Panel

Next I am going to create a new image and save it in the Media Panel. Of course an Editor can also upload the image but I want to do it from code.

This is a really interesting post about working with Media on EPiServer World:
//world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/75/Content/Assets-and-media/Working-with-media/

Media (for example, files) are treated as any other content type but have an associated binary data. The binary data is stored using something called BLOB providers which are optimized for storing streams of data.

The code below is an example of how to save an image as a blob and make it available in the Media Panel under ‘For This Site’.

Now lets say you want to put it in a different folder. For example put it in the subfolder ‘Startpage’. Lets adjust the code it bit and use what I described in the previous section for finding this folder.

First I created a method to find the ContentReference of a subfolder with a specific name:

Now we can use this to set the target folder:

That’s it. Now we can save an Image in any folder we like in the Media Panel.

 

Upload an File through a webform and save it in the Media Panel

First lets create a form that can be used to upload images (or text files or whatever you want).

In the controller lets create a method that catches this submission and saves the uploaded file in a folder.

Remember I am testing this code in the Alloy MVC Sample that has an GenericMedia type. This Media Type accepts all file types and therefor the uploading works. Normally its not recommended to have such mediatype. Use attribute [MediaDescriptor(ExtensionString = “txt,zip,pdf”)] on a Media Type to specify exactly which filetypes are allowed.

 

Related links about this topic

Leave a Comment.