Copied to clipboard

Flag this post as spam?

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


  • Eranga Wijethunga 30 posts 171 karma points
    Jan 11, 2023 @ 04:40
    Eranga Wijethunga
    0

    Umbraco 11 - Create media from Stream

    I'm developing a package for the new Umbraco 11 version, In there I need to create media and get its URL using byte stream. Seems it's a bit different from the older version such as version 7/8. Can anybody suggest me way of doing it or clear documentation or an article that I can get some support?

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 12, 2023 @ 09:44
    Huw Reddick
    0

    Hi Eranga,

    Without knowing what method you are currently using, do the new docs not help?

    https://docs.umbraco.com/umbraco-cms/reference/management/services/mediaservice

  • Eranga Wijethunga 30 posts 171 karma points
    Jan 12, 2023 @ 11:07
    Eranga Wijethunga
    0

    In Umbraco 7, I'm using the following method and its working fine

       private void SaveMediaUsingMediaService(string sourceImageName, out IMedia newMedia, int parentFolderId, byte[] mediaBuffer, string mediaType, out string tempurl, string fileName)
            {
                newMedia = _mediaService.CreateMedia(sourceImageName, parentFolderId, mediaType);
                tempurl = "";
                newMedia.SetValue("umbracoFile", sourceImageName, new MemoryStream(mediaBuffer));
                _mediaService.Save(newMedia);
                if (mediaType == "File")
                {
                    tempurl = newMedia.GetValue("umbracoFile").ToString();
                }
                else
                {
                    dynamic mediaInfo = JsonConvert.DeserializeObject(newMedia.GetValue("umbracoFile").ToString());
                    tempurl = mediaInfo.src;
                }
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
            }
    

    This is the one I'm trying to use on Umbraco version 11

    This just create dummy file on media section but content empty as below image enter image description here

      private void SaveMediaUsingMediaService(string sourceImageName, out IMedia newMedia, int parentFolderId, byte[] mediaBuffer, string mediaType, out string tempurl, string fileName)
            {
                newMedia = _mediaService.CreateMedia(sourceImageName, parentFolderId, mediaType);
                tempurl = "";
                newMedia.SetValue("umbracoFile", fileName);
                _mediaService.Save(newMedia);
                if (mediaType == "File")
                {
                    tempurl = newMedia.GetValue("umbracoFile").ToString();
                }
                else
                {
                    dynamic mediaInfo = JsonConvert.DeserializeObject(newMedia.GetValue("umbracoFile").ToString());
                    tempurl = mediaInfo.src;
                }
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
            }
    
  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 13, 2023 @ 12:19
    Huw Reddick
    0

    Did you try the code example in the link I posted? (it does not appear to be the same code as you are currently trying)

  • Eranga Wijethunga 30 posts 171 karma points
    Jan 13, 2023 @ 12:39
    Eranga Wijethunga
    0

    Hi Huw, Yes I tried But SetValue Method signature bit different in Umbraco 11 than mentioned in the documentation it accepts only these parameters.

    SetValue(string propertyTypeAlias, object? value, string? culture = null, string? segment = null);
    

    But I need to set the value for the media URL. If I parse only the first two parameters it only creates a dummy file in Umbraco media, not the actual image I added.

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 13, 2023 @ 13:23
    Huw Reddick
    0

    you don't appear to be passing the stream in the setvalue call (last value passed in the method, after filename.

        media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, "unicorn.jpg", stream);
    
  • Eranga Wijethunga 30 posts 171 karma points
    Jan 16, 2023 @ 10:03
    Eranga Wijethunga
    0

    Yes, but about method signature is different. It doesn't accept stream

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 16, 2023 @ 10:44
    Huw Reddick
    0

    Not sure what you are doing then, as the SetValue method does accept a stream in Umbraco 11, exactly as shown in the example from the documentation.

    There are 2 overloads for that method

    enter image description here

  • Eranga Wijethunga 30 posts 171 karma points
    Jan 17, 2023 @ 06:36
    Eranga Wijethunga
    0

    Thanks, but I can't see two overloads in IContentBase Interface. That is the issueenter image description here

    Please correct me if you see I missed anything. Also if you need I can show you my full method

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 17, 2023 @ 07:24
    Huw Reddick
    0

    Full method would help since you should have an Imedia object not Icontentbase

  • Eranga Wijethunga 30 posts 171 karma points
    Jan 17, 2023 @ 14:02
    Eranga Wijethunga
    0

    Hi Huw, I'm creating IMedia object. Please check my method

    private void SaveMediaUsingMediaService(string sourceImageName, out IMedia newMedia, int parentFolderId, byte[] mediaBuffer, string mediaType, out string tempurl, string fileName)
    {
    
        try
        {
            newMedia = _mediaService.CreateMedia(sourceImageName, parentFolderId, mediaType);
            tempurl = "";
            newMedia.SetValue("umbracoFile", new MemoryStream(mediaBuffer));
            _mediaService.Save(newMedia);
            if (mediaType == "File")
            {
                tempurl = newMedia.GetValue("umbracoFile").ToString();
            }
            else
            {
                dynamic mediaInfo = JsonConvert.DeserializeObject(newMedia.GetValue("umbracoFile").ToString());
                tempurl = mediaInfo.src;
            }
            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }
        }
        catch (Exception ex)
        {
            tempurl = "";
            newMedia=null;
        }
    }
    
  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 17, 2023 @ 14:19
    Huw Reddick
    0

    you are not passing in the correct values which is why it is defaulting to the wrong overload, you need to pass ALL the correct values as shown in the example and in my post above, one of those is the stream, you can't just pass in 2 values like you are attempting.

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 17, 2023 @ 14:35
    Huw Reddick
    0

    Method signature for SetValue

       public static void SetValue(this Umbraco.Cms.Core.Models.IContentBase content, 
        Umbraco.Cms.Core.IO.MediaFileManager mediaFileManager,
        Umbraco.Cms.Core.PropertyEditors.MediaUrlGeneratorCollection mediaUrlGenerators, 
        Umbraco.Cms.Core.Strings.IShortStringHelper shortStringHelper, 
        Umbraco.Cms.Core.Services.IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, 
        string propertyTypeAlias, 
        string filename, 
        System.IO.Stream filestream, 
        [string culture = null], 
        [string segment = null])
    
                    Member of Umbraco.Extensions.ContentExtensions
    
                Summary:
                Sets the posted file value of a property.
    
  • Eranga Wijethunga 30 posts 171 karma points
    Jan 18, 2023 @ 09:53
    Eranga Wijethunga
    0

    Parsed parameters the way you mentioned in the previous reply(Please see attached image) but now it's giving an error saying

    error CS1501: No overload for method 'SetValue' takes 10 arguments enter image description here

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 18, 2023 @ 10:00
    Huw Reddick
    0

    remove _contentBase from you method call, it is passed explicitly because it is an extension method, hence the this in the signature definition.

    so call it as below.

    media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, "unicorn.jpg", stream);
    
  • Eranga Wijethunga 30 posts 171 karma points
    Jan 18, 2023 @ 10:17
    Eranga Wijethunga
    0

    Thanks Huw , I had tried this but it still says

    No overload for method 'SetValue' takes 7 arguments

     newMedia.SetValue(_mediaFileManager, 
                            _mediaUrlGeneratorCollection, 
                            _shortStringHelper,
                            _contentTypeBaseService, 
                            Constants.Conventions.Media.File, 
                            "unicorn.jpg", 
                            stream);
    

    Did you try this previously Umbraco version 11?

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 18, 2023 @ 10:46
    Huw Reddick
    100

    Yes, I have tested it in Umbraco v11, here is a simple controller class which I tested with, it succesfully creates a media file in the backoffice.

        public class MediaTestSurfaceController : SurfaceController
        {
            private IMediaService _mediaService;
            private IWebHostEnvironment _webHostEnvironment;
            private MediaFileManager _mediaFileManager;
            private MediaUrlGeneratorCollection _mediaUrlGeneratorCollection;
            private IShortStringHelper _shortStringHelper;
            private IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
    
            public MediaTestSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider
            ,IMediaService mediaService, IWebHostEnvironment webHostEnvironment,
            MediaFileManager mediaFileManager, MediaUrlGeneratorCollection mediaUrlGeneratorCollection,IShortStringHelper shortStringHelper, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider
            ) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
            {
                _mediaService = mediaService;
                _webHostEnvironment = webHostEnvironment;
                _mediaFileManager = mediaFileManager;
                _mediaUrlGeneratorCollection = mediaUrlGeneratorCollection;
                _shortStringHelper = shortStringHelper;
                _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
            }
    
            public void MediaTest()
            {
                string webRootPath = _webHostEnvironment.WebRootPath;
                var path = Path.Combine(webRootPath, "wizard.png");
    
    // Open a new stream to the file
                using (Stream stream = System.IO.File.OpenRead(path))
                {
                    // Initialize a new image at the root of the media archive
                    IMedia media = _mediaService.CreateMedia("Unicorn", Constants.System.Root, Constants.Conventions.MediaTypes.Image);
                    // Set the property value (Umbraco will handle the underlying magic)
                    media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, "unicorn.png", stream);
    
                    // Save the media
                    var result = _mediaService.Save(media);
                }
            }
        }
    
  • Eranga Wijethunga 30 posts 171 karma points
    Jan 18, 2023 @ 10:50
    Eranga Wijethunga
    0

    Thanks. Let me try this on a sample project and let you know the results

  • Eranga Wijethunga 30 posts 171 karma points
    Jan 19, 2023 @ 10:22
    Eranga Wijethunga
    0

    Hi Huw, I was trying to implement this function in the separate class library and it has some other functions as well. After moving the saving function to a separate class this is working fine.

    Thanks a lot for your support, Now I'm trying to identify what is the actual reason for not working with the same class previously added. I will update the thread after I found the reason. Thanks again.

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 19, 2023 @ 10:37
    Huw Reddick
    0

    Glad you got it working.

  • Hannes Lilljequist 1 post 71 karma points
    Apr 06, 2023 @ 10:16
    Hannes Lilljequist
    0

    I had the exact same issue and for me the problem was that I was passing a Task<Stream> instead of a Stream as the last argument. Still haven't been able to test this properly, so I'll check back if I've spoken too soon.

Please Sign in or register to post replies

Write your reply to:

Draft