Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James Lau 37 posts 58 karma points
    Nov 21, 2017 @ 09:56
    James Lau
    0

    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.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 21, 2017 @ 15:27
    Dan Diplo
    0

    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
            }
    
        }
    }
    
  • James Lau 37 posts 58 karma points
    Nov 27, 2017 @ 04:02
    James Lau
    0

    Finally, I use these functions to get a stream:

        private IMedia GetMedia(int id)
        {
            var context = ApplicationContext.Current;
            var services = context.Services;
            var mediaService = services.MediaService;
    
            return mediaService.GetById(id);
        }
    
        private ToStream(IMedia media)
        {
            var ms = new MemoryStream();
    
                var umbracoFile = media.GetValue<string>(Constants.Conventions.Media.File);
                var url = JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFile).Src;
    
                var path = Server.MapPath(url);
    
                if (System.IO.File.Exists(path))
                {
                    using (Stream input = System.IO.File.OpenRead(path))
                    {
                        input.CopyTo(ms);
                    }
                }
    
    
            ms.Position = 0;
            return ms;
        }
    

    But, it cannot retrieve image stored as blob using plugin UmbracoFileSystemProviders.Azure.

Please Sign in or register to post replies

Write your reply to:

Draft