Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    May 21, 2014 @ 11:09
    Jason Espin
    0

    Required properties for creating an image with the Media service

    Hi all,

    I posted an original question here:

    http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/52982-Using-the-Umbraco-media-services-to-create-a-media-item-from-an-image-uploaded-to-a-specified-directory

    Since then, I have made some ground on retrieving the image from a folder based upon the supplied path and creating a new image using System.Drawing.Image however I'm now stuck as to which property of the media object I pass this into.

    My code can be found below:

    public partial class MediaServiceTest : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        try
        {
          string originalPath = "uploadedImages/test.png";
          IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
          var newImage = mediaService.CreateMedia("myNewImage", -1, "Image");
          System.Drawing.Image image = System.Drawing.Image.FromFile(System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath(originalPath)));
          newImage.SetValue("umbracoFile", image);
          mediaService.Save(newImage);
        }
        catch (Exception ex)
        {
          var message = ex;
        }
    
      }
    }
    

    As you can see I have tried to pass the image into umbracoFile but this doesn't seem to have worked and returns the following error:

    "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments"
    

    Any help would be greatly appreciated.

    Regards,

    Jason

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 21, 2014 @ 11:19
    Dave Woestenborghs
    0

    Hi,

    I think you can set newImage.Path = "path to your file". And then saving should do the trick.

    dave

  • Jason Espin 368 posts 1335 karma points
    May 21, 2014 @ 14:19
    Jason Espin
    104

    Hi Dawoe,

    Unfortunately, it's not as simple as that and that code doesn't actually do anything. Eventually I managed to crack it using this code:

        string originalPath = "uploadedImages\\test.png";
        IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
        var newImage = mediaService.CreateMedia("myTestImage", -1, "Image");
        byte[] buffer = System.IO.File.ReadAllBytes(System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath(originalPath)));
        System.IO.MemoryStream strm = new MemoryStream(buffer);
        newImage.SetValue("umbracoFile", "myNewImage.png", strm);
        mediaService.Save(newImage);
        if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(originalPath)))
        {
          System.IO.File.Delete(HttpContext.Current.Server.MapPath(originalPath));
        }
    
  • Chris Ashton 57 posts 89 karma points
    Dec 28, 2015 @ 22:45
    Chris Ashton
    0

    This worked for me, thanks Jason.

    Chris

  • Helen Zitomer 2 posts 22 karma points
    Oct 06, 2014 @ 20:02
    Helen Zitomer
    0

    Thank you Jason, I'm trying to do something similar and your example is very helpful.  But I'm running into a glitch - I cannot find a method to check if a media item with the same name exists before I call mediaService.CreateMedia - the only media services methods I've found are GetById and GetMediaByPath, and all I have is the name (like "myTestImage") and the ancestor folder id.

    And, if one traverses from an ancestor folder, what property does one check for a name match?

    I'm doing this in a .cs file, making me wish I could use uQuery...but, no.

     

     

  • Jason Espin 368 posts 1335 karma points
    Oct 08, 2014 @ 13:43
    Jason Espin
    0

    Hi Helen,

    If I understand your question correctly, you are looking to check that a media item with a given name does not exist before creating a new media item in Umbraco. To do this, you can traverse from an ancestor folder and use a bit of Linq to solve your problem.

    int ancestorFolderId = 2039;
    IEnumerable<Imedia> images = ms.GetChildren(ancestorFolderId).Where(x => x.Name == "TheImageName");
    // Or use .GetDescendants
    

    This essentially asks Umbraco to give you all of the children of your ancestor folder that have the name "TheImageName". Typically you will only have one image named this so the IEnumerable

    if(images.Count() > 0){
        // Image already exists
        // Access the image and its properties by using images.First()
    }else{
        //Image does not exist. Create a new one.
    }
    

    Always ensure that you check you IEnumerable has content before trying to use it though as it will error if you don't.

  • Helen Zitomer 2 posts 22 karma points
    Oct 08, 2014 @ 21:25
    Helen Zitomer
    0

    Excellent!  exactly what I needed.  thanks!

  • Maff 141 posts 466 karma points
    Feb 02, 2015 @ 23:05
    Maff
    0

    Thanks for sharing this Jason - you just helped me avoid a few hours of head scratching tonight! :)

    Maff

  • MuirisOG 382 posts 1284 karma points
    Jun 30, 2015 @ 12:27
    MuirisOG
    0

    Thanks for sharing, Jason, this is excellent.

    My question is how do you use this if you have created your own Media Type, with Image as a component.

    [EDIT]

    I found it - I must use CreateMedia but specify my own Media Type instead of "Image"

    e.g.

    var newImage = mediaService.CreateMedia("myTestImage", -1, "myImageType");
    
Please Sign in or register to post replies

Write your reply to:

Draft