Copied to clipboard

Flag this post as spam?

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


  • Kasper 13 posts 93 karma points
    27 days ago
    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 339 posts 1781 karma points MVP c-trib
    27 days ago
    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