HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Media examples

Upload and update media files programmatically using BLOB providers, IContentRepository, and IBlobFactory.

Media (such as files) are treated as any other content type but have associated binary data stored using BLOB providers, which are optimized for storing data streams.

The following example defines a simple content type used in subsequent examples:

[ContentType]
public class GenericFile : MediaData {
  public string Description {
    get;
    set;
  }
}

Upload a file

Create a file with binary data by getting a default content instance, writing to a BLOB, and publishing.

public void Uploading_a_file() {
  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var blobFactory = ServiceLocator.Current.GetInstance<IBlobFactory>();

  //Get a new empty file data
  var file1 = contentRepository.GetDefault<GenericFile>(SiteDefinition.Current.GlobalAssetsRoot);
  file1.Name = "Readme.txt";

  //Create a blob in the binary container
  var blob = blobFactory.CreateBlob(file1.BinaryDataContainer, ".txt");
  using(var s = blob.OpenWrite()) {
    StreamWriter w = new StreamWriter(s);
    w.WriteLine("Hello world");
    w.Flush();
  }

  //Assign to file and publish changes
  file1.BinaryData = blob;
  var file1ID = contentRepository.Save(file1, SaveAction.Publish);
}

Upload media by file extension

Resolve the correct media type at runtime from the file extension using ContentMediaResolver.

public void Uploading_a_media_from_extension() {
  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
  var mediaDataResolver = ServiceLocator.Current.GetInstance<ContentMediaResolver>();
  var blobFactory = ServiceLocator.Current.GetInstance<IBlobFactory>();

  //Get a suitable MediaData type from extension
  var mediaType = mediaDataResolver.GetFirstMatching(".txt");
  var contentType = contentTypeRepository.Load(mediaType);

  //Get a new empty file data
  var media = contentRepository.GetDefault<MediaData>(SiteDefinition.Current.GlobalAssetsRoot, contentType.ID);
  media.Name = "Readme.txt";

  //Create a blob in the binary container
  var blob = blobFactory.CreateBlob(media.BinaryDataContainer, ".txt");
  using(var s = blob.OpenWrite()) {
    StreamWriter w = new StreamWriter(s);
    w.WriteLine("Hello world");
    w.Flush();
  }

  //Assign to file and publish changes
  media.BinaryData = blob;
  var file1ID = contentRepository.Save(media, SaveAction.Publish);
}

Update file metadata

Retrieve an existing file as a writable clone, modify its properties, and publish.

public void UpdatingMetaData_of_a_file() {
  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var blobFactory = ServiceLocator.Current.GetInstance<IBlobFactory>();

  //the file to update, not hardcoded of course
  var fileID = new ContentReference(444);

  //Get the file
  var file1 = contentRepository.Get<GenericFile>(fileID).CreateWritableClone() as GenericFile;

  //Update description
  file1.Description = "My description";

  //Publish
  var file1ID = contentRepository.Save(file1, SaveAction.Publish);

}

Update file binary data

Replace the binary data of an existing file by creating a new BLOB and assigning it.

public void UpdatingBinary_of_a_file() {
  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var blobFactory = ServiceLocator.Current.GetInstance<IBlobFactory>();

  //the file to update, not hardcoded of course
  var fileID = new ContentReference(444);

  //Get the file
  var file1 = contentRepository.Get<GenericFile>(fileID).CreateWritableClone() as GenericFile;

  //Upload new blob
  var blob = blobFactory.CreateBlob(file1.BinaryDataContainer, ".txt");
  using(var s = blob.OpenWrite()) {
    StreamWriter w = new StreamWriter(s);
    w.WriteLine("Hello world");
    w.Flush();
  }

  //Assign to file and publish changes
  file1.BinaryData = blob;
  var file1ID = contentRepository.Save(file1, SaveAction.Publish);
}

See BLOB storage and providers to change the storage model for media.