Copied to clipboard

Flag this post as spam?

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


  • Kasper 20 posts 110 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 from codeshare.co.uk 356 posts 1851 karma points MVP 2x c-trib
    Mar 26, 2019 @ 12:17
    Paul Seal from codeshare.co.uk
    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.

Please Sign in or register to post replies

Write your reply to:

Draft