Copied to clipboard

Flag this post as spam?

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


  • Leo Jebran 4 posts 84 karma points
    Oct 15, 2021 @ 08:36
    Leo Jebran
    0

    Creating a new media type (Image) with media service Umbraco 9

    I am trying to save images that the a member that can upload from the front end as a media node with the Media service

    in the view I post to a surface controller

    @using (Html.BeginUmbracoForm<MyController>("Add",null,Model))
    {
            ......
    
            <input asp-for="myfile" type="file"/>
            <span asp-validation-for="myfile"></span>
    
            <div>
                <button type="submit">Add</button>
            </div>
        </div>
    }
    

    in the post method

    if (model.myfile != null)
    {
          using (Stream stream = model.myfile.OpenReadStream())
          {
                 IMedia media = _mediaService.CreateMedia(model.myfile.FileName, -1, Image.ModelTypeAlias);
                        media.SetValue("umbracoFile",model.myfile); //problem here
                _mediaService.Save(media);
    
          //from doc the SetValue method takes a stream 
    
          }    
    }
    

    the media.SetValue method takes propertyTypeAlias and object which is different from the documentation example here

    the media node does get created but the images isn't displayed right

    enter image description here

    any idea would be appreciated

    please and thank you

  • Søren Gregersen 427 posts 1814 karma points MVP 2x c-trib
    Oct 15, 2021 @ 10:13
    Søren Gregersen
    0

    Hi,

    The code in the example is a bit different from yours. Your are, at least, missing the filestream argument, as far as I can see.

  • Keith 44 posts 180 karma points
    Oct 16, 2021 @ 19:09
    Keith
    100

    The method you are using only sets the value of the "umbracoFile" property on the new media node (which is a string path to the file on the disk). It doesn't actually try to save the file iteself.

    You could in theory manually try to save the file to the disk, then get the path of the file and set that as the value for "umbracoFile". But I dont think that is recommended.

    There is an extension method that does all the necessary saving of the file creating random path names, setting the path on the media etc. It takes the file as a Stream. Its not very pretty because it takes a bunch of other services as parameters too. So you would need to inject those services and convert your posted file to a stream with something like this:

    using Umbraco.Extensions;
    

    Inject the following services into your controller:

        private readonly MediaFileManager mediaFileManager;
        private readonly MediaUrlGeneratorCollection mediaUrlGenerators;
        private readonly IShortStringHelper shortStringHelper;
        private readonly IContentTypeBaseServiceProvider contentTypeBaseServiceProvider;
    

    Then convert your posted file into a stream and call the extension method something like this (i dont use posted files so this is untested, but it works for me when I get streams from elsewhere)

        using (MemoryStream stream = new MemoryStream(model.myfile.FileBytes))
        {
            media.SetValue(mediaFileManager, mediaUrlGenerators, shortStringHelper, contentTypeBaseServiceProvider, Constants.Conventions.Media.File, model.myfile.FileName, stream);
        }
    
  • Leo Jebran 4 posts 84 karma points
    Oct 16, 2021 @ 22:06
    Leo Jebran
    0

    Hey Keith

    Thanks for the great explanation, it worked!

    the file is of type IFromFile

    so here is what I did, if anyone might be interested

    await using (var ms = new MemoryStream())
    {
         await model.myfile.CopyToAsync(ms);
    
         var media = _mediaService.CreateMedia(model.myfile.FileName, -1, Image.ModelTypeAlias);
    
         media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, model.myfile.FileName, ms);
    
         _mediaService.Save(media);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft