Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I'm hooking into ContentServicePublished and I need to get at the actual media of a node - expressly to get the MimeType and read the bytes from disk/http to create a CouchDb Attachment).
I can get 'close' with
var media = ApplicationContext.Current.Services.MediaService.GetById(mediaid);
var media2 = helper.Media(mediaid);
//where helper is an Umbraco.Web.UmbracoHelper
but this just gets me a Media object with bunch of properties - I can see the url
but that looks like a URL rather than I file. I guess I can cludge something together to read it from the filesystem by pretending its a path relative to AppDomain.CurrentDomain.BaseDirectory but that feels like a terrible mistake, especially if its configured to store it on Azure or Amazon!
I could read the URL with webclient, but I don't know how to get the current hostname?
I feel I'm missing something obvious: time to ask!
This may help you, try to look at /Settings/Media Types/Image and see the Generic properties.
You can use alias "umbracoExtension" and "umbracoBytes".
That's not quite helping. the umbracoBytes contains the length and umbracoFile gets me the filename (path in fact) and umbracoExtension gets the file extension (so I can use that to product a Mime Type) but I want to read the file itself. What is the best way to read the content of the file into memory, given all I have is a relative path?
i.e. is there a public class/method that is used by the umbraco backend that is aware of things like UmbracoFileSystemProviders in order to get the file itself (whether it is stored locally or via https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure for instance.)
Having spent the evening reading some Umbraco source code, I think what I need is IFileSystem. There are probably enough clues in the Umbraco.Web.Editors.ImagesController to see what I need to do.
As (16 months later) I was looking for this (again) here the the answer I came up with ...
public class SheetIOMediaReader
public byte Read(IMedia med)
if (med != null)
var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
var imagePath = med.Properties["umbracoFile"].Value.ToString();
var fullOrgPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(imagePath));
byte res = null;
using (var fileStream = mediaFileSystem.OpenFile(fullOrgPath))
if (fileStream.CanSeek) fileStream.Seek(0, 0);
byte buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
res = ms.ToArray();
I am using Umbraco 7.5.13. The IMedia "umbracoFile" property value returned is a JSON string. It must be parsed.
dynamic jsonValue = JObject.Parse(imagePath);
string propertyValue = jsonValue.src.Value;
var relativePath = mediaFileSystem.GetRelativePath(propertyValue);
var fullOrgPath = mediaFileSystem.GetFullPath(relativePath);
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted