Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 11, 2020 @ 17:45
    Ismail Mayat
    0

    Create image using media service outside web context

    I am using https://github.com/callumbwhyte/sidecar and looking to create images using media service.

    The bit I am stuck on is according to umbraco docs https://our.umbraco.com/Documentation/Reference/Management/Services/MediaService/Index/ when creating image you give it HttpPostedFileBase file however as i am not posting anything I am actually reading an image from a folder what do i pass in media.SetValue is it base64 encoded image string?

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 11, 2020 @ 17:55
    Ismail Mayat
    0

    Ok so got a bit further,

     var imageFiles = Directory.GetFiles(_config.ProfileImageFileLocation);
    
            foreach (var file in imageFiles)
            {
                var fileItem = new FileInfo(file);
    
                Console.WriteLine($"uploading file {fileItem.Name}");
    
                var media = _mediaService.CreateMedia(fileItem.Name, _config.ProfileImagesFolderId,
                    _config.MediaTypeAlias);
    
                using (FileStream fileStream = fileItem.OpenRead())
                {
                    media.SetValue(Constants.Conventions.Media.File, fileStream);
                }
                _mediaService.Save(media);
            }
    

    However I do not see the image itself, I am using blob storage so dont know if that has something todo with it or whether it does not like filestream being passed?

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 11, 2020 @ 19:09
    Ismail Mayat
    0

    Hmm to image is set to System.Io.Filestream. So maybe base64 encoded is required?

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Feb 11, 2020 @ 23:25
    Nik
    0

    Have a look at the imagefilter content app. That has the ability to save files so might help.

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 12, 2020 @ 08:10
    Ismail Mayat
    0

    So after a little advice from Ronald on slack https://our.umbraco.com/Documentation/Reference/Management/Services/MediaService/Index/#creating-a-new-media-item-from-a-stream

    I updated my code so it now looks like:

                    var fileItem = new FileInfo(file);
    
                Console.WriteLine($"uploading file {fileItem.Name}");
    
                using (var fileStream = fileItem.OpenRead())
                {
                    IMedia media = _mediaService.CreateMedia(fileItem.Name, _config.ProfileImagesFolderId,
                        _config.MediaTypeAlias);
    
                    media.SetValue(_contentTypeBaseServiceProvider,Constants.Conventions.Media.File, fileItem.Name,fileStream);
                    _mediaService.Save(media);
                }
    

    This has got a bit further in that in media section the source of the image is correct its an actual url as opposed to what i had before "System.Io.Filestream" however the image still gives 404

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies