Copied to clipboard

Flag this post as spam?

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

  • Henning Ejnefjäll 7 posts 87 karma points
    May 27, 2019 @ 07:23
    Henning Ejnefjäll

    Save images/files to media folder programmatically

    Hi! I've recently started developing with Umbraco and I'm currently using the latest version (version 8). The experience has been good so far but I've stumbled across a problem and I just can't seem to find any working solution for it using version 8...

    I'm simply trying to programmatically save a file (image) uploaded through a regular asp form (Html.BeginUmbracoForm(...)). The file information (filename) seem to get stored in the database but when I look in the media folder it's not there and I don't get any error messages saving it (in the backoffice I see the reference/filename is stored both on page and under the media folder but the actual image or information about width etc. is not). Here's a part of my code for storing the file:

    //myFile is an object of HttpPostedFileBase
    IMediaService mediaService = Services.MediaService; //creating service
    int mediaRootId = 4120; //pointing to sub folder under media
    var filename = myFile.FileName; 
    var mediaType = Constants.Conventions.MediaTypes.Image;
    var media = mediaService.CreateMedia(filename, mediaRootId, mediaType);
    media.SetValue("umbracoFile", myFile.InputStream);

    And later I save the media object to a page property together with other form data...

    content.SetValue("myUploadedFile", media);
    var result = contentService.SaveAndPublish(content);

    I've been scratching my head and I'm thankful for any help regarding this...

  • Henning Ejnefjäll 7 posts 87 karma points
    Jun 04, 2019 @ 13:10
    Henning Ejnefjäll

    Anyone here with some insight about this?

  • Mario Lopez 167 posts 930 karma points MVP 3x c-trib
    Jun 04, 2019 @ 22:27
    Mario Lopez

    Hi Henning,

    There's an extension method in the Umbraco.Core namespace that accepts an HttpPostedFileBase directly, so you can do this to save your uploaded file:

     media.SetValue(Services.ContentTypeBaseServices, "umbracoFile", "filename1.png", myFile);

    The ContentTypeBaseServices is available in the Services object of an Umbraco controller.

  • Henning Ejnefjäll 7 posts 87 karma points
    Jun 05, 2019 @ 07:07
    Henning Ejnefjäll

    Great! Works like a charm... Many thanks Mario!

    media.SetValue(Services.ContentTypeBaseServices, "umbracoFile", myFile.FileName, myFile.InputStream);
  • Josip 195 posts 662 karma points c-trib
    Jun 04, 2019 @ 23:01

    Hi Mario, do you know how to make it visible in Umbraco backoffice?

  • Henning Ejnefjäll 7 posts 87 karma points
    Jun 20, 2019 @ 08:41
    Henning Ejnefjäll

    Incase you're still struggling with this Josip... Saving your page/content and file like this should work for you and display it in the backoffice:

    IContentService contentService = Services.ContentService;
    var content = contentService.CreateContent(pageName, udiOfParent, "myContentType"); 
    content.SetValue(Services.ContentTypeBaseServices, "myFile", someFile.FileName, someFile.InputStream);
  • Mario Lopez 167 posts 930 karma points MVP 3x c-trib
    Jun 04, 2019 @ 23:43
    Mario Lopez

    Try creating the media with CreateMediaWithIdentity instead:

    var media = Services.MediaService.CreateMediaWithIdentity(filename, mediaRootId, mediaType);
    media.SetValue("umbracoFile", myFile);
  • David Armitage 498 posts 2063 karma points
    Jun 21, 2020 @ 03:40
    David Armitage


    This is how I do it.

     public IMedia InsertImage(HttpPostedFileBase file, int mediaFolderId, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
                    IMedia media = _mediaService.CreateMedia(file.FileName, mediaFolderId, Constants.Conventions.MediaTypes.Image);
                    media.SetValue(contentTypeBaseServiceProvider, "umbracoFile", file.FileName, file);
                    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.



  • wilmar 19 posts 129 karma points
    Jan 22, 2021 @ 10:32

    Thanks you are my hero !! :) i can add something here if you want to save this media on a media picker you can do it like this

     var media =  InsertImage(myHttpPostedFileBase);
     var member =  GetCurrentIMember();
            var udi = Udi.Create(DefinedConstants.UdiEntityType.Media, media.Key);
  • Fernando 13 posts 82 karma points
    Jul 07, 2022 @ 21:06

    Hi, I am new in Umbraco (I am using v9). What is Services here? IMediaService mediaService = Services.MediaService; //creating service


  • Frederik Sørensen 3 posts 73 karma points
    Mar 28, 2023 @ 06:40
    Frederik Sørensen

    Hi Fernando

    Yes, you are correct.

    In v9 the Services.MediaService is now Dependency Injected as IMediaService

    // Fred

  • Fernando 13 posts 82 karma points
    Mar 28, 2023 @ 11:24

    Yes, I solved this using DI, thanks.

Please Sign in or register to post replies

Write your reply to: