Copied to clipboard

Flag this post as spam?

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


  • Kasper 21 posts 112 karma points
    Mar 26, 2019 @ 08:14
    Kasper
    0

    Upload Image to media

    Hi Guys, I am trying to upload a Image, but I get this error.
    enter image description here

  • Paul Seal 425 posts 2327 karma points MVP 3x c-trib
    Mar 26, 2019 @ 12:17
    Paul Seal
    0

    You could use a method like this:

        private IMedia CreateMediaItem(int parentId, string fileName, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
        {
            IMedia newFile = _mediaService.CreateMedia(fileName, parentId, "Image");
            string filePath = HttpContext.Current.Server.MapPath("~/img/" + fileName);
            using (FileStream stream = System.IO.File.Open(filePath, FileMode.Open))
            {
                newFile.SetValue(contentTypeBaseServiceProvider, "umbracoFile", fileName, stream);
            }
            _mediaService.Save(newFile);
            return newFile;
        }
    

    And call it like this:

    var contentTypeBaseServiceProvider = Current.Services.ContentTypeBaseServices;
    IMedia myImage = CreateMediaItem(parentId, "myImage.jpg", contentTypeBaseServiceProvider);
    

    And you should be able to inject the IContentTypeBaseServiceProvider into your controller instead of using Current. But see if this works first.

  • Mathias 41 posts 173 karma points
    Jun 28, 2019 @ 11:30
    Mathias
    0

    This does not work, for starters there isn't a overload on IMedia.SetValue() that takes a ContentTypeBaseServiceProvider.

    I've tried with the following:

     private IMedia CreateMediaItem(int parentId, HttpPostedFileBase file)
        {
            var mediaService = Services.MediaService;
    
            IMedia newFile = mediaService.CreateMedia(file.FileName, parentId, "Image");
            newFile.SetValue("umbracoFile", file);
    
            mediaService.Save(newFile);
    
            return newFile;
        }
    

    But this hasn't worked either. I have no idea what to do with the file in order for it to be correctly added to the Media library.

  • Kimbrough 9 posts 119 karma points
    Jun 28, 2019 @ 18:46
    Kimbrough
    0

    Take a look at this post.

  • Morten Leerhøy 11 posts 82 karma points
    Jun 28, 2019 @ 12:35
    Morten Leerhøy
    0

    You need to reference Umbraco.Core - the setvalue method that takes ContentTypeBaseServiceProvider resides in Umbraco.Core.ContentExtensions

  • David Armitage 369 posts 1561 karma points
    Jun 21, 2020 @ 03:40
    David Armitage
    0

    Hi,

    This is how I do it.

     public IMedia InsertImage(HttpPostedFileBase file, int mediaFolderId, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
            {
                try
                {
                    IMedia media = _mediaService.CreateMedia(file.FileName, mediaFolderId, Constants.Conventions.MediaTypes.Image);
                    media.SetValue(contentTypeBaseServiceProvider, "umbracoFile", file.FileName, file);
                    _mediaService.Save(media);
                    return media;
                }
                catch (Exception e)
                {
                    _logger.Error<UmbMediaService>("InsertImage | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                }
    
                return null;
            }
    

    And this is how I call the method.

    var contentTypeBaseServiceProvider = Current.Services.ContentTypeBaseServices;
                            IMedia image = _umbMediaService.InsertImage(model.Logo, model.MediaFolderId, contentTypeBaseServiceProvider);
    

    Here are the using statements. I am sure not all are necessary. I am sure your visual studios intellisesne will tell you if one of them is not required.

    using Umbraco.Core.Composing;
    using Website.Core.Helpers;
    using Website.Core.Models;
    using Website.Core.Models.EntityModels;
    using Website.Core.Services; 
    

    I can confirm this code works. I literally just re-tested this on a project before posting.

    Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft