Aaron Sadler
Posted by:

Aaron Sadler

Category

Umbraco

Uploading a file into Umbraco 8 has changed slightly, using the code below as found on the Umbraco Help Forums, worked for me:

Now you need the IContentTypeBaseServiceProvider and a Stream.

// Umbraco 8
using Umbraco.Core; <- Important..
// Needed Interfaces
IMediaService _mediaService; 
IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
// The new way to do it.
IMedia media = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", Stream);
_mediaService.Save(media);

Byte[]

byte[] byteArray;
using (Stream stream = new MemoryStream(byteArray))
{
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", stream);
}

HttpPostedFileBase

HttpPostedFileBase httpPostedFileBase;
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", httpPostedFileBase.FileName, httpPostedFileBase.InputStream);

Thanks to Bo Jacobsen for sharing the solution!

Comments

Post a comment

Fields marked with an * (asterisk) are required


Recent Posts

Tips & Tricks
How to use Cloudflare Workers and Transform Rul...

This post explains how to m...

News
UmbHost Limited is now a Silver Umbraco Partner

We are now officially a Sil...

Umbraco
How to pass a Content Security Nonce (CSP) to G...

How to use a CSP nonce with...

News
UmbCheckout 1.0.0 & UmbCheckout.StarterKit.Stri...

The stable version of UmbCh...

News
Voting is now open for the Green Business of th...

We've been shortlisted! - P...

ADVERTISTING
Browse Umbraco Hosting

Umbraco Hosting Starting At £20.00/month