How to check if image exists based on file path

Ron Rainey asked on March 15, 2018 21:58

Fairly new to Kentico working on a news site. I have a transformation that grabs a byline associated with an article and uses it to construct a file path to the reporters photo. I do it this way because the photo is not an object associated with the article node but it does reside in the kentico file structure. This works great: <%# IfEmpty(Eval("byline"), "", "<img id='mugshot' src='/App_Themes/style/shared/images/reportermugs/"+Eval("byline")+".png'/>") %>

BUT I need to test if the file exists first so that it is not trying to fetch a file that doesn't exist. I've tried several combinations of the image and boolean operation methods but there doesn't seem to be a way to do this in an ASCX transformation.

Is there a way in an ASCX transformation to use a path string to check if a file exists at that location or do I need to go about this in an entirely different way?

Correct Answer

Peter Mogilnitski answered on March 16, 2018 01:04

ACSX transformation is essentialy a user control, so you can do something like this:

<script runat="server">
    bool fileExists =  false;
    string path = "";

    protected override void OnInit(EventArgs e)
    {
        if (!String.NullOrEmpty(Eval("byline")) {
            path = "/App_Themes/style/shared/images/reportermugs/" 
                                                + Eval("byline") + ".png";

            if (System.IO.File.Exists( System.Web.Server.MapPath("~" + path)) {
                 fileExists = true;
            }
        }   
    }
 </script>

<%# IfTrue(fileExists , "<img id='mugshot' src='" + path + "'/>")%>

P.S. Didn't check the code but it will give you the idea

0 votesVote for this answer Unmark Correct answer

Recent Answers


Ron Rainey answered on March 16, 2018 21:20 (last edited on March 16, 2018 21:46)

Hi Peter, Thanks so much for the help. Realizing I can go in and do this with C# is very helpful (though I am new to that as well)

Based on your code I managed to get the following working:

bool fileExists = false;
string path = "";
protected override void OnInit(EventArgs e)
{
   if (String.IsNullOrEmpty(Eval("byline").ToString()) == false) {
        path = "/App_Themes/style/shared/images/reportermugs/"+ Eval("byline") + ".png";
        if (System.IO.File.Exists(Server.MapPath("~" + path))) {
            fileExists = true;
        }
    }   
    else
    {
        Console.WriteLine("Null or empty");
    } 
}

Your code threw some errors so I had to tweak it a bit. And it's not the most elegant with that extra else in there but it kept throwing "no NullorFalse option declared" until I did.

I'm not real clear on what some parts of this line mean: protected override void OnInit(EventArgs e)

Or the details of exactly how this maps: System.IO.File.Exists(Server.MapPath() As I had to remove.System.Web before it would work

So I have a lot of learning to do.

Thank you very much for the help. UPDATE: Actually I was able to remove that else condition when I changed the IsNullOrEmpty == false. that cleared the error.

0 votesVote for this answer Mark as a Correct answer

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