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 524 posts 2889 karma points MVP 7x 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 51 posts 205 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 11 posts 141 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 510 posts 2081 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

  • Kasper 13 posts 84 karma points
    Jan 14, 2021 @ 09:13
    Kasper
    0

    I was struggling with this while trying to write a plugin that would allow fetching images from a web url and storing them in a folder. I ended up solving it in Umbraco 8.10.1 using the following code:

    public bool SaveImageByUrl(string url, IMedia mediaFolder, string fileName)
        {
            try
            {
                IMedia media = Services.MediaService.CreateMediaWithIdentity(fileName, mediaFolder, Constants.Conventions.MediaTypes.Image);
    
                WebClient wc = new WebClient();
                using (MemoryStream memoryStream = new MemoryStream(wc.DownloadData(url)))
                {
                    media.SetValue(_contentTypeBaseServiceProvider, Constants.Conventions.Media.File, fileName, memoryStream);
                    media.SetValue("alternativeText", "demo");
    
                    Services.MediaService.Save(media);
                }
                    return true;
            }
            catch (System.Exception ex)
            {
                Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Error ocurred while importing image.", ex);
                throw ex;
            }
        }
    

    Note that the _contentTypeBaseServiceProvider has to be imported via dependency injection in the constructor of your controller.

    If you need to create the stream from a local file on disk, you can instead use File.OpenRead("path-to-your-file").

    These are my import statements:

    using Umbraco.Core; 
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    using Web.Models;
    using Constants = Umbraco.Core.Constants;
    
Please Sign in or register to post replies

Write your reply to:

Draft