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
    0

    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);
    mediaService.Save(media);
    

    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
    0

    Anyone here with some insight about this?

  • Mario Lopez 168 posts 958 karma points MVP 4x c-trib
    Jun 04, 2019 @ 22:27
    Mario Lopez
    104

    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
    0

    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
    Josip
    0

    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
    0

    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 168 posts 958 karma points MVP 4x c-trib
    Jun 04, 2019 @ 23:43
    Mario Lopez
    0

    Try creating the media with CreateMediaWithIdentity instead:

    var media = Services.MediaService.CreateMediaWithIdentity(filename, mediaRootId, mediaType);
    media.SetValue("umbracoFile", myFile);
    Services.MediaService.Save(media);
    
  • 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

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

    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);
            member.SetValue("mycustomimage",udi.ToString());
            _memberService.Save(member);
    
  • Fernando 13 posts 82 karma points
    Jul 07, 2022 @ 21:06
    Fernando
    0

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

    Thanks.

  • Frederik Sørensen 6 posts 76 karma points
    Mar 28, 2023 @ 06:40
    Frederik Sørensen
    0

    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
    Fernando
    0

    Yes, I solved this using DI, thanks.

Please Sign in or register to post replies

Write your reply to:

Draft