Copied to clipboard

Flag this post as spam?

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


  • Terry Clancy 204 posts 944 karma points
    Mar 15, 2016 @ 07:28
    Terry Clancy
    0

    Umbraco dynamic mediaItem url property has extra unwanted charaters in the string

    Hi all,

    The following code:

        @{ 
                dynamic mediaItem = new DynamicMedia(product.ThumbnailImageMediaId);
                // string imageSource = @mediaItem.umbracoFile;
                string imageSource = @mediaItem.url;
            <a href="@url"><img src="@imageSource" /></a>
        }
    

    Sets imageSource to

    "{src: '/media/1012/box_ms_office_pro_2013_1pc1user_thumb.png', crops: []}"
    

    But if the html img tag is to work, it should be just the path as follows:

    "/media/1012/box_ms_office_pro_2013_1pc1user_thumb.png"
    

    without the extra ""{src:" at the beginning and ", crops: []}"" at the end.

    Can anyone please help me understand what is happening and/or how to fix it ?

    Thanks very much

    Terry Clancy

    ClanceZ

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Mar 15, 2016 @ 07:52
    Dennis Adolfi
    1

    Try this:

    @{
            var mediaItem = Umbraco.Media(product.ThumbnailImageMediaId);
            <a href="@url"><img src="@mediaItem.umbracoFile" /></a>
        }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 15, 2016 @ 09:39
    Dave Woestenborghs
    0

    Hi,

    Looking at this you are using the image cropper on your media item.

    You can do

    @mediaItem.GetCropUrl(100,100) 
    

    To display a image of 100x100

    Dave

  • Terry Clancy 204 posts 944 karma points
    Mar 15, 2016 @ 19:24
    Terry Clancy
    0

    Dave and Dennis,

    Thanks very much ... FYI I am using Umbraco version 7.4.1

    Dennis Adolfi:

    Thankyou. Trying to use code you suggest:

    @{
            var mediaItem = Umbraco.Media(product.ThumbnailImageMediaId);
            <a href="@url"><img src="@mediaItem.umbracoFile" /></a>
        }
    

    On Umbraco.Media I get

    "The type or namespace media does not exist in the namespace Umbraco, (are you missing an assembly reference?).

    What DLL do I need to reference please - based on https://our.umbraco.org/Documentation/Reference/Management/Models/Media I tried to reverence Umbraco.Core.DLL and added

    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    

    But that did not help ?

    Dave Woestenborghs:

    Thankyou :-) I have not intentionally or knowingly used "Image cropper on your media item" . Note that and Umbraco BackOffice Media / Image /Properties shows Media Type as "Image".

    When I try to include the code you suggest:

    dynamic mediaItem = new DynamicMedia(product.ThumbnailImageMediaId);
    string imageSource = @mediaItem.GetCropUrl(100,100);
    <a href="@url"><img src="@imageSource" /></a>
    

    imageSource returns an empty string ?? Also using debugger mediaItem does not show a property or method "GetCropUrl" ??

    Any help with either of these two approaches to solve the problem would be appreciated.

    Terry Clancy

    ClanceZ

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Mar 15, 2016 @ 19:49
    Dennis Adolfi
    100

    The @Umbraco.Media comes from the Umbraco.Web.UmbracoHelper class. What does your view inherit from? In my example my view inherited UmbracoTemplatePage. Are you using a custom model? You should be able to find the solution on this documentation: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

  • Terry Clancy 204 posts 944 karma points
    Mar 15, 2016 @ 20:33
    Terry Clancy
    0

    Hi Denis and THANKs again.

    Ahhh I see ....... FYI ... my code is part of a uCommerce Helper created for WebForms but which I am using in a MVC app (although many of my pages are from the uCommerce code and do not follow MVC coding practice - those pages are typically included as partial views without models or controllers, not perfect but works). That (I think) is why I have not inherited from UmbracoTemplatePage. I resolved this from info on https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/ with

    var umbracoHelper = new Umbraco.Web.UmbracoHelper
    

    So then coding as you suggested:

                var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);      
                var mediaItem = umbracoHelper.Media(product.ThumbnailImageMediaId);
                string imageSource = (string) mediaItem.umbracoFile;
    

    I got the error: "Cannot implicitly convert type 'Umbraco.Web.Models.ImageCropDataSet' to 'string' "

    But with a minor modification using mediaItem.Url instead of mediaItem.umbracoFile it all works:

                var umbracoHelper = new Umbraco.Web.UmbracoHelper           (Umbraco.Web.UmbracoContext.Current);
                var mediaItem = umbracoHelper.Media(product.ThumbnailImageMediaId);
                // string imageSource = (string) mediaItem.umbracoFile;
                string imageSource = (string)mediaItem.Url;
    

    Thanks for your assistance resolving this. It is still unclear why this happened as this code previously worked. I suspect a breaking change in recent versions of Umbraco? Anyway all good now, thanks very much Denis.

    Terry Clancy

    ClanceZ

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Mar 15, 2016 @ 20:41
    Dennis Adolfi
    0

    Perfect, glad to hear that it worked out for you Terry!

    Much appriciated if you would mark my post as solution.

    Have as great night!

  • Terry Clancy 204 posts 944 karma points
    Mar 16, 2016 @ 02:35
    Terry Clancy
    1

    Thanks again and will do !!

    Terry Clancy

    ClanceZ

  • 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