I'm trying to write some code that creates and uploads new media. I can see that the media service allows me to create the media node, but how do I upload the actual data bytes the media node represents. I've seen some sample code writing files directly to the filesystem, but that seems a little clunky, and will presumably fail if the installation is using an Azure filesystem provider or similar.
Is there an abstraction i'm missing in order to upload the actual bytes representing the media file?
var media = _mediaService.CreateMedia(mediaName, mediaFolder.Id, Constants.MediaTypes.Image);
media.SetValue("umbracoFile", mediaName, stream);
_mediaService.Save(media);
_mediaService is of IMediaService which you can get from the ApplicationContext.Current.Services
Of for stream, depends on your implementation, where you get the image from, in our case, it was from db (don't ask...), and we'd be using a MemoryStream to load the image from db.
Uploading new media
Hi All
I'm trying to write some code that creates and uploads new media. I can see that the media service allows me to create the media node, but how do I upload the actual data bytes the media node represents. I've seen some sample code writing files directly to the filesystem, but that seems a little clunky, and will presumably fail if the installation is using an Azure filesystem provider or similar.
Is there an abstraction i'm missing in order to upload the actual bytes representing the media file?
TIA
Hi Andrew,
It should be enough to use
_mediaService
is ofIMediaService
which you can get from theApplicationContext.Current.Services
Of for
stream
, depends on your implementation, where you get the image from, in our case, it was from db (don't ask...), and we'd be using aMemoryStream
to load the image from db.Hope this sample helps.
--Dirk
That sounds perfect. We're generating Excel spreadsheets on the fly, so turning those into a stream is trivial. I'll give that a try shortly!
Works perfectly. Many many thanks! H5YR
is working on a reply...