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 63 karma points
    Jan 24, 2014 @ 03:52
    Hugh
    2

    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 1348 posts 8385 karma points MVP 2x 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 13 posts 93 karma points
    27 days ago
    Kasper
    0

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

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

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

Please Sign in or register to post replies

Write your reply to:

Draft