How to upload a file to umbraco 8

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!

 

Configure 404 Errors in Azure with Umbraco

In Azure you will need to add a line to your web.config to allow the Umbraco 404 error page to work, otherwise your users will be greeted with the following message:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

The change you need to make in your web.config is as follows:

<configuration>
  ...
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  ...
  </system.webServer>
  ...
</configuration>