Copied to clipboard

Flag this post as spam?

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


  • Hugh 30 posts 64 karma points
    Jan 24, 2014 @ 03:52
    Hugh
    3

    Create Media Programmatically in V7

    Can anyone help me with some code to create images programmatically in V7 API?  I have got the Node stuff sorted using Umbraco.Core.Serivices.ContentService, but the media stuff is not being so nice.

    Esentially, I have created a user control to which people can create nodes, then upload images to associate with the node.  I want to add these images to the Media section.

    Thanks in advance.

    Hugh

  • Kevin Jump 2348 posts 14896 karma points MVP 8x c-trib
    Jan 24, 2014 @ 13:08
    Kevin Jump
    7

    Hi i've done something very similar in uSync.ContentEdition so the source might shed light on it

    https://github.com/KevinJump/jumoo.usync/blob/master/jumoo.usync.content/MediaImporter.cs#L85

    and

    https://github.com/KevinJump/jumoo.usync/blob/master/jumoo.usync.content/helpers/FileHelper.cs#L236

    the create is simple enough

    _mediaService = ApplicationContext.Current.Services.MediaService;
    media = _mediaService.CreateMedia(name, parentId, mediaAlias);
    

    the import media file (in FileHelper above) was more tricky.

    you need to call IMediaItem.SetValue and pass the filename and stream to upload the umbracoFile

    FileStream s = new FileStream(file, FileMode.Open);
    item.SetValue("umbracoFile", Path.GetFileName(file), s);
    s.Close();
    

    all of this assumes you haven't done something 'special' with the umbracoFile property.

    hope that helps.

  • Kasper 21 posts 112 karma points
    Mar 26, 2019 @ 08:18
    Kasper
    0

    Hi Kevin, I am trying to upload a Image, but I get this error.
    enter image description here

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Jan 24, 2014 @ 17:33
    Sebastiaan Janssen
    7

    Make sure to call _mediaService.Save(media); after that.

  • Dhanesh Kumar MJ 167 posts 543 karma points MVP c-trib
    Jun 16, 2020 @ 03:52
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies