Cannot access direct URL of image

Eric Conklin asked on August 20, 2021 17:30

I am using the media gallery web part and am trying to access the direct URL of the file.

I want to use a Lightbox addon but I cannot seem to figure out how to get the direct URL. I am trying the GetMediaFileURL and passing in the GUID using an Eval expression and the name of the current website but I cannot figure it out.

Here is my transformation:

    <%@ Register Src="~/CMSModules/MediaLibrary/Controls/LiveControls/MediaFilePreview.ascx" TagName="MediaFilePreview" TagPrefix="cc1" %>
<div class="mediaItem">
<table>
    <tr>
      <h3><%# HTMLEncode(LimitLength(GetNotEmpty("FileTitle;FileName"), 18, "...")) %></h3>
        <td class="mediaLibraryPhoto"><a href="<%# HTMLHelper.HTMLEncode(MediaLibraryFunctions.GetMediaFileUrl(Eval("GUID"), "Hospice of the Western Reserve")) %>" title="<%# ResHelper.GetString(Convert.ToString(Eval("FileDescription"))) %>">
        <cc1:MediaFilePreview ID="filePreview" runat="server" maxsidesize="700"  /></a>
        </td>
    </tr>
    <tr>
        <td class="mediaLibraryDescription">
<a href="<%# HTMLHelper.HTMLEncode(MediaLibraryFunctions.GetMediaFileDetailUrl(Eval("FileID"))) %>" title="<%# ResHelper.GetString(Convert.ToString(Eval("FileDescription"))) %>">

            </a><br/>



        </td>
    </tr>
</table>
<div class="mediaItemBottom"></div>
</div>

Do I need to add an additional method to this transformer? If so, any help is appreciated.

Thanks.

Recent Answers


kentico guy answered on August 20, 2021 21:22 (last edited on August 20, 2021 21:43)

EDIT: actually this returns an img tag not just the URL so I don't think it's what you're looking for.

I had a similar problem recently and I couldn't find a out of box method for this so I just created my own macro method for it.

[MacroMethod(typeof(string), "Returns img tag from a guid", 1)]
[MacroMethodParam(0, "Image guid", typeof(string), "The image guid to convert into an img tag")]
[MacroMethodParam(1, "Image width", typeof(int), "The width int to size in, NULLABLE")]
[MacroMethodParam(2, "Image height", typeof(int), "The height int to size in, NULLABLE")]
 public static object GetImageTagByGuid(EvaluationContext context, params object[] parameters)
{
    object width = null;
    object height = null;
    if (parameters.Length > 1)
    {
        if (parameters[1] != null)
        {
            width = Convert.ToInt32(parameters[1]);
        }
    }
    if (parameters.Length > 2)
    {
        if (parameters[2] != null)
        {
            height = Convert.ToInt32(parameters[2]);
        }
    }
    string imgTag = CMS.DocumentEngine.Web.UI.CMSTransformation.Methods.GetImage("image-rendition", parameters[0], null, width, height);
    // this part strips to just the url but it seems highly inefficient
    imgTag = imgTag.Replace("<img src=/"", "").Replace("/"/>","");
    return imgTag;
}

in order to use custom macro methods you'd have to switch to text/xml afaik. I don't think you can use custom macro methods in ascx but maybe I'm wrong

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.