SaaS CMS has officially launched! Learn more now.

Upload/Create file programmatically to the media asset system



I have a bunch of files (mostly pdf and docx files) on the harddrive that I want to add to my EPiServer 8 site programmatically. I'm having some troubles. Here is what I've tried:

var contentRepository = ServiceLocator.Current.GetInstance();
var blobFactory = ServiceLocator.Current.GetInstance();

var file1 = contentRepository.GetDefault(SiteDefinition.Current.GlobalAssetsRoot);
var container = file1.BinaryDataContainer;
var blob = blobFactory.CreateBlob(container, ".docx");

using (var fs = new FileStream("c:\\test.docx", FileMode.Open))

file1.BinaryData = blob;
file1.Name = "test.docx";
var file1ID = contentRepository.Save(file1, SaveAction.Publish);

A file called "test.docx" is created/added to Episerver but it only contains jibberish and not the actual content from the existing file. What am I doing wrong here?

Jun 30, 2015 13:01

Can you try to use IContentTypeRepository & MediaDataResolver:

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();

var mediaDataResolver = ServiceLocator.Current.GetInstance<ContentMediaResolver>();


var mediaType = mediaDataResolver.GetFirstMatching(".jpg");
var contentType = contentTypeRepository.Load(mediaType);

var file1 = contentRepository.GetDefault<GenericMediaFile>(SiteDefinition.Current.GlobalAssetsRoot, contentType.ID);
Jun 30, 2015 13:32


Just by reading your code it looks like it should work. Does it make any difference if you add the FileAccess.Read and FileShare.Read parameters when you create the FileStream instance?

using (Stream fs = new FileStream("C:\\test.docx", FileMode.Open, FileAccess.Read, FileShare.Read) 
Jun 30, 2015 13:40

I would replace the last line in Quan's code with:

var file1 = contentRepository.GetDefault<IContentMedia>(SiteDefinition.Current.GlobalAssetsRoot, contentType.ID);
Jun 30, 2015 13:42

Thank you for the answers. I have tried all your suggestions but no difference in the result.

The thing is that it works fine if I try with an existing textfile instead, but as soon as I work with something more advanced (docx) It doesn't work so good (See screenshot. Ignore the license message, I haven't downloaded a dev license yet :))

So I suppose I have to do something special with more advanced file types?

EDIT: I got an epiphany now. Maybe it has to do with the missing license. Since that is appending to the content the file might get corrupt?

Edited, Jun 30, 2015 14:44


I would download a dev license and try it again. It's probably caused by the license exception being injected.

UPDATE: I just noticed you realized it all by yourself. :)

Edited, Jun 30, 2015 14:54

Yes - that is it. The reason was when you download the file - docx, our license system injects the warning in to the response - which is supposed to be an HTML response.

Placing a license should solve your problem. It should always be advised to develop with working license - no hidden headache :)

Have a nice day.


Jun 30, 2015 14:59

Thanks for your time guys. Sometimes you just need to ask the questions to realize what the answer is :) It now works with the license. I should stop being lazy.. ;)

Edited, Jun 30, 2015 15:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.