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. Here you will also find information about how to work with media programmatically.
Examples
The following code defines a simple content type that will be used in the examples:
C#
[ContentType]
public class GenericFile : MediaData
{
public string Description { get; set; }
}
And then creates a new file and defines a simple txt.file with the message “Hello World’:
C#
public void Uploading_a_file()
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();
var file1 = contentRepository.GetDefault<GenericFile>(SiteDefinition.Current.GlobalAssetsRoot);
file1.Name = "Readme.txt";
var blob = blobFactory.CreateBlob(file1.BinaryDataContainer, ".txt");
using (var s = blob.OpenWrite())
{
StreamWriter w = new StreamWriter(s);
w.WriteLine("Hello world");
w.Flush();
}
file1.BinaryData = blob;
var file1ID = contentRepository.Save(file1, SaveAction.Publish);
}
In the following example a file is created where the actual file type is resolved during runtime from extension:
C#
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<BlobFactory>();
var mediaType = mediaDataResolver.GetFirstMatching(".txt");
var contentType = contentTypeRepository.Load(mediaType);
var media = contentRepository.GetDefault<MediaData>(SiteDefinition.Current.GlobalAssetsRoot, contentType.ID);
media.Name = "Readme.txt";
var blob = blobFactory.CreateBlob(media.BinaryDataContainer, ".txt");
using (var s = blob.OpenWrite())
{
StreamWriter w = new StreamWriter(s);
w.WriteLine("Hello world");
w.Flush();
}
media.BinaryData = blob;
var file1ID = contentRepository.Save(media, SaveAction.Publish);
}
In the following example an existing file is updated with new metadata:
C#
public void UpdatingMetaData_of_a_file()
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();
var fileID = new ContentReference(444);
var file1 = contentRepository.Get<GenericFile>(fileID).CreateWritableClone() as GenericFile;
file1.Description = "My description";
var file1ID = contentRepository.Save(file1, SaveAction.Publish);
}
In the following example an existing file is updated with new binary data:
C#
public void UpdatingBinary_of_a_file()
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();
var fileID = new ContentReference(444);
var file1 = contentRepository.Get<GenericFile>(fileID).CreateWritableClone() as GenericFile;
var blob = blobFactory.CreateBlob(file1.BinaryDataContainer, ".txt");
using (var s = blob.OpenWrite())
{
StreamWriter w = new StreamWriter(s);
w.WriteLine("Hello world");
w.Flush();
}
file1.BinaryData = blob;
var file1ID = contentRepository.Save(file1, SaveAction.Publish);
}
Do you find this information helpful? Please log in to provide feedback.