locked
New to ASP.NET checking if image exists vb RRS feed

  • Question

  • User394159258 posted

    Completely new to ASP.net so bear with me...

    Page Language="vb"

    Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082


    I want to check on the server for the existance of an image, then if it does exist, display the image wrapped in other code.


    eg

    if "/images/name.jpg" exists then

    <div><img src="/images/name.jpg"></div>


    else

    Do nothing.


    Seems really simple to me but NOTHING I have tried has worked (compilation errors).

    I've tried examples using system.io, DIM stuff with 'FileInfo', Set fs.

    FileInfo examples produce 'not defined' errors, 'Set' says it's outdated and not used anymore and so on.

    Other examples I've seen I couldn't attempt at trying since they were whole pages of code. Surely it can't be THAT difficult, can it?

    TIA

    Tuesday, January 19, 2010 12:15 PM

Answers

  • User-1772898816 posted

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.IO" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            DoesFileExist("Images/logo.gif")
        End Sub
        
        Function DoesFileExist(ByVal strFileName As String) As Boolean
            If File.Exists(Server.MapPath(strFileName)) Then
                image1.ImageUrl = strFileName
            End If
        End Function
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Image ID="image1" runat="server" />
        </div>
        </form>
    </body>
    </html>


    You can do something like what is above except use an array or a list of the image names and then pass them into the function and then test.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2010 7:59 AM
  • User1006193418 posted

    Hi,

    You may also be interested in IMG tag's onerror event. Please refer to these links for demoes and description:

    http://docstore.mik.ua/orelly/web/jscript/refp_171.html

    http://www.logiclabz.com/javascript/replacement-image-in-html-img-tag-using-onerror-property.aspx.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 22, 2010 5:38 AM

All replies

  • Tuesday, January 19, 2010 12:34 PM
  • User-507673006 posted

    You could try using an asp:Image control like the following:

    <asp:Image ID="Image1" runat="server" />

    Then use something along the lines of this in your code-behind:

    Dim imageStr As String = "~/images/name.jpg"
    If System.IO.File.Exists(imageStr) Then
    Image1.ImageUrl = "~/images/name.jpg"
    End If


    I havn't tested this but something along those lines should work...
     

    Tuesday, January 19, 2010 12:39 PM
  • User394159258 posted

    http://www.vb-helper.com/howto_net_file_exists.html This produces the error: BC30289: Statement cannot appear within a method body. End of method assumed.

    And

    Dim imageStr As String = "~/images/name.jpg"  
    If System.IO.File.Exists(imageStr) Then  
    Image1.ImageUrl = "~/images/name.jpg"  
    End If  

    <asp:Image ID="Image1" runat="server" />

    Runs ok but doesn't product anything at all on the page.

    Wednesday, January 20, 2010 4:09 AM
  • User-68639941 posted

     hi,

    refer below code

    	if (System.IO.File.Exists(Server.MapPath("~/FOLDER03.ICO")))
    		{
    			//set the image path , to access the html img control at server-side it should be marked with runat="server" attribute
    		}


     

    Wednesday, January 20, 2010 5:08 AM
  • User394159258 posted

    I've got the following working:

    <%@ Import Namespace="System.IO" %>
    <script language="VB" runat="server">
        Dim strFileThatExists As String = "/frontads/meindl.jpg"

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            litFileThatExists.Text = DoesFileExist(strFileThatExists)
        End Sub

        Function DoesFileExist(strFileName As String) As Boolean
            Dim strFullPath As String
            If InStr(strFileName, ":") <> 0 Then
                strFullPath = strFileName
            Else
                strFullPath = Server.MapPath(strFileName)
            End If
            DoesFileExist = File.Exists(strFullPath)
        End Function
    </script>


    <%= File.Exists(Server.MapPath(strFileThatExists)) %>


    BUT I want to add an, if File.Exists == True then

    <div><img src="strFileThatExists1.jpg"/></div>

    (building up to checking for multiple images in sequence, 1.jpg, 2.jpg etc)


    Wednesday, January 20, 2010 5:21 AM
  • User-1772898816 posted

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.IO" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            DoesFileExist("Images/logo.gif")
        End Sub
        
        Function DoesFileExist(ByVal strFileName As String) As Boolean
            If File.Exists(Server.MapPath(strFileName)) Then
                image1.ImageUrl = strFileName
            End If
        End Function
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Image ID="image1" runat="server" />
        </div>
        </form>
    </body>
    </html>


    You can do something like what is above except use an array or a list of the image names and then pass them into the function and then test.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2010 7:59 AM
  • User1006193418 posted

    Hi,

    You may also be interested in IMG tag's onerror event. Please refer to these links for demoes and description:

    http://docstore.mik.ua/orelly/web/jscript/refp_171.html

    http://www.logiclabz.com/javascript/replacement-image-in-html-img-tag-using-onerror-property.aspx.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 22, 2010 5:38 AM