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 20, 2014 @ 17:36
    Jason Espin
    0

    Using the Umbraco media services to create a media item from an image uploaded to a specified directory

    Hi all,

    I am some difficulty using the Umbraco media services to create usable media objects, particularly of media type "Image".

    As a bit of background, we have a piece of software that allows our clients to define products, their description, associated images and other information that can then be accessed via a web service and displayed within our Umbraco powered site. I am using this web service in Umbraco to create content pages based upon the data retrieved from the web service which is actually fairly simple. My main issue though is processing the associated images

    An associated image in the web service is literally defined as a path to an image on the users machine such as:

    "C:\Users\jason\My Pictures\Test\test.jpg"
    

    Clearly this image cannot be sent as a stream via the web service so I will be allowing our users to upload the images via FTP to a folder within the Umbraco directory named "uploadedImages".

    When the automated web service call is run in Umbraco (nightly), the local path will be converted so that it relates to an image that has been uploaded to the server. So

    "C:\Users\jason\My Pictures\Test\test.jpg"
    

    would become

      "\uploadedImages\Test\test.jpg" 
    

    What I then wish to do is convert this image to a Media item in Umbraco so that it becomes:

    "\Media\Test\1001\test.jpg"
    

    I would then clear all files from the 'uploadedImages' folder as this would no longer be required.

    So for example the following shows the structure of the 'uploadedImages' folder as of today:

    /uploadedImages
        /argentina
           /102456.jpg
           /102457.jpg
           /102458.jpg
        /uruguay
           /203123.jpg
           /204560.jpg
           /205698.jpg
    

    After parsing, the media folder structure would look something like this

    /Media
       /argentina
          /1001
            /102456.jpg
            /102456_thumb.jpg
          /1002
            /102457.jpg
            /102457_thumb.jpg
    ...
    

    My main issue is I don't know how to pass the image from it's location in 'uploadedImages' into the Media service to achieve this output.

    Currently I have the following where 'image' is a dummy path I have defined myself:

    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.Message;
      }
    
    }
    

    What I need to know is how I then pass the file located here:

       /uploadedImages
           /argentina
               /102456.jpg
    

    into the MediaService.

    I cannot find anything relating to this in the documentation or on the forums so any help would be greatly appreciated.

Please Sign in or register to post replies

Write your reply to:

Draft