I am using MediaService to retrieve an IMedia object. How to load the file into memory stream and the physical file may store in file system, Azure blob, or network folder?
If you can I'd use IPublishedContent rather than IMedia as it deals better with the abstractions of getting a URL to the media. Once you have the relative URL you can map that to an absolute path on disk - not sure how this works for Azure blob as I don't use it - but it works for stuff in media folder. Once you have an absolute path you can read it into a stream and copy it to a MemoryStream.
Something like:
var media = Umbraco.TypedContent(1234); // your media Id
if (media != null)
{
string path = Server.MapPath(media.Url);
if (System.IO.File.Exists(path))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (Stream input = System.IO.File.OpenRead(path))
{
input.CopyTo(memoryStream);
}
memoryStream.Position = 0;
// you can access your memory stream here
}
}
}
How to load IMedia object to memorystream
I am using MediaService to retrieve an IMedia object. How to load the file into memory stream and the physical file may store in file system, Azure blob, or network folder?
Thanks for help.
If you can I'd use
IPublishedContent
rather thanIMedia
as it deals better with the abstractions of getting a URL to the media. Once you have the relative URL you can map that to an absolute path on disk - not sure how this works for Azure blob as I don't use it - but it works for stuff in media folder. Once you have an absolute path you can read it into a stream and copy it to aMemoryStream
.Something like:
Finally, I use these functions to get a stream:
But, it cannot retrieve image stored as blob using plugin UmbracoFileSystemProviders.Azure.
is working on a reply...