Copied to clipboard

Flag this post as spam?

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


  • David Armitage 510 posts 2082 karma points
    Jul 12, 2016 @ 09:38
    David Armitage
    0

    How to get Umbraco Image Full File Path

    Hi,

    Please can anyone help. I have been fiddling around far too much trying to figure this out. Any help would be awesome :)

    I just want to full file page of an umbraco image Eg C:\Dropbox\Web\sycacouk\Source Code\Website\media\1078\testimage.png

    I have manged to write code for a Media item like pdf but the same code just doesn't work for an image

    This is the code I used for a file like PDF

    public static string GetMediaFilePathById(int Id)
            {
                try
                {
                    IMedia media = MediaManager.GetMediaFileById(Id);
                    var umbracoFilePath = media.GetValue<string>(Umbraco.Core.Constants.Conventions.Media.File);
                    var path = HttpContext.Current.Server.MapPath(umbracoFilePath);
                    return path;
                }
                catch
                {
                    return string.Empty;
                }
            }
    

    I got errors if I passed an image Id rather than a file Id. I attempted to modify it slightly but no job.

    Eg

    var umbracoFilePath = media.GetValue<string>(Umbraco.Core.Constants.Conventions.MediaTypes.Image);
    

    I googled around and found loads of examples that were old or didn't work. the closet I got to what I want is.

    public static string GetImageFilePathById(int Id)
            {
                try
                {
                    IMedia media = MediaManager.GetMediaFileById(Id);
                    var fullPath = media.GetValue("umbracoFile").ToString();
                    return fullPath;
                }
                catch(Exception e)
                {
                    return string.Empty;
                }
            }
    

    but this returned a strange string (Not what I want)

    {src: '/media/1079/id3232-peter-pan_com.png', crops: []}
    

    Anyone got any idea how to get the full file path either from this string or any other way?

    Much Appreciated

    David

  • David Peck 690 posts 1896 karma points c-trib
    Jul 12, 2016 @ 11:04
    David Peck
    103

    Hi David (great name!!!)

    Try this:

    try
    {
        IMedia media = MediaManager.GetMediaFileById(Id);
        var umbracoFile = media.GetValue<string>(Constants.Conventions.Media.File);
        return JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFile).Src;
     }
     catch(Exception e)
     {
         return string.Empty;
     }
    

    You're probably missing a few namespaces there, so if you can't work them out then you can probably grab what you need from my unrelated post from which I grabbed this source code.

    David

  • David Armitage 510 posts 2082 karma points
    Jul 12, 2016 @ 11:30
    David Armitage
    0

    Hi Dave,

    Awesome! Thanks for your help. You save me a heap of time Googling this again tomorrow.

    This is exactly what I needed. Just needed to add the MapPath around but nothing major.

    public static string GetImageFilePathById(int Id)
            {
                try
                {
                    IMedia media = MediaManager.GetMediaFileById(Id);
                    var umbracoFile = media.GetValue<string>(Constants.Conventions.Media.File);
                    string path = HttpContext.Current.Server.MapPath(JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFile).Src);
                    return path;
                }
                catch (Exception e)
                {
                    return string.Empty;
                }
            }
    

    Thanks again. Hope I can re-pay the favor something!

    Kind Regards

    David

  • 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