?
Solved

How to display images in a ASP without hardcoding?

Posted on 2003-02-25
5
Medium Priority
?
327 Views
Last Modified: 2008-03-03
Hi,
I wish to create a dynamic ASP page wherein, the page should list out all the pictures in that folder. I dont want to hardcode every picture name into the page. As I add pictures to the folder, they should be reflected in the page.
Please help me...
0
Comment
Question by:digitally1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
zeheb earned 100 total points
ID: 8017596
<%  
    Dim FileSystemObject
    Dim FolderHandler
    Dim File
   
    Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")
    Set FolderHandler = FileSystemObject.GetFolder("C:\Inetpub\webpub\Test")
    For Each File In FolderHandler.Files
        If InStr(1, File, ".gif", 1) <> 0 Or InStr(1, File, ".jpg", 1) Then
           Response.Write("<IMG SRC='" & File & "'>")
        End If
    Next
    Set FolderHandler = Nothing
    Set FileSystemObject = Nothing
%>
0
 

Assisted Solution

by:Bw
Bw earned 100 total points
ID: 8024420
a very nice one from asp101:
It will also handle directories and so on just place this code into your image home dir and call it default.asp

regards,
   Bw

<%
'*******************************************************
'*     ASP 101 Sample Code - http://www.asp101.com     *
'*                                                     *
'*   This code is made available as a service to our   *
'*      visitors and is provided strictly for the      *
'*               purpose of illustration.              *
'*                                                     *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
'* BW: adjusted to MP style                            *


%>

<%
' This function takes a filename and returns the appropriate image for
' that file type based on it's extension.  If you pass it "dir", it assumes
' that the corresponding item is a directory and shows the folder icon.
Function ShowImageForType(strName)
      Dim strTemp

      ' Set our working string to the one passed in
      strTemp = strName
      
      ' If it's not a directory, get the extension and set it to strTemp
      ' If it is a directory, then we already have the correct value
      If strTemp <> "dir" Then
            strTemp = LCase(Right(strTemp, Len(strTemp) - InStrRev(strTemp, ".", -1, 1)))
      End If
      
      ' Debugging line used to perfect that above string parser
      'Response.Write strTemp
      
      ' Set the part of the image file name that's unique to the type of file
      ' to it's correct value and set this to strTemp. (yet another use of it!)
      Select Case strTemp
            'Case "asp"
            '      strTemp = "asp"
            'Case "dir"
            '      strTemp = "dir"
            'Case "htm", "html"
            '      strTemp = "htm"
            Case "gif", "jpg"
                  strTemp = "img"
            'Case "txt"
            '      strTemp = "txt"
            'Case Else
            '      strTemp = "misc"
      End Select

      ' All our logic is done... build the IMG Tag for display to the browser
      ' Place it into... where else... strTemp!

      ' My images are all GIFs and all start with "dir_" for my own sanity.
      ' They end with one of the values set in the select statement above.

      strTemp = "<IMG SRC=""./images/dir_" & strTemp & ".gif"" WIDTH=16 HEIGHT=16 BORDER=0>"

      ' Set return value and exit function
      ShowImageForType = strTemp
End Function
'That's it for functions on this one!
%>


<%' Now to the Runtime code:
Dim strPath   'Path of directory to show
Dim objFSO    'FileSystemObject variable
Dim objFolder 'Folder variable
Dim objItem   'Variable used to loop through the contents of the folder

' You could just as easily read this from some sort of input, but I don't
' need you guys and gals roaming around our server so I've hard coded it to
' a directory I set up to illustrate the sample.
' NOTE: As currently implemented, this needs to end with the /
strPath = "./"

' Create our FSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

' Get a handle on our folder
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))

' Show a little description line and the title row of our table
%>

<TABLE BORDER="0"  CELLSPACING="0" CELLPADDING="2">
      <TR >
            <TD>Name:</TD>
            <TD>Size:</TD>
            <TD>Created:</TD>
            <TD>Type:</TD>
      </TR>
<%

' First I deal with any subdirectories.  I just display them and when you
' click you go to them via plain HTTP.  You might want to loop them back
' through this file once you've set it up to take a path as input.  It seems
' like the logical thing to do to me at least!
For Each objItem In objFolder.SubFolders
      ' Deal with the stupid VTI's that keep giving our visitors 404's
      If InStr(1, objItem, "_vti", 1) = 0 Then

            dim size: size = objItem.Size
            dim dateCreated: dateCreated = objItem.DateCreated
            dim objType: objType = objItem.Type

            intEndPos = Instr(dateCreated, " ")
            dateCreated = Mid(dateCreated, 1,intEndPos)
      
            dim t: t=" b"
            if size>1024 then
                  size = round(size/1024,0)
                  t="Kb"
                  if size>1024 then
                        size = round(size/1024,0)
                        t="Mb"
                  end if
            end if
            size = size & t
                  



            %>
            <TR>
                  <TD ALIGN="left" ><A HREF="<%= strPath & objItem.Name %>/default.asp">>> <%= objItem.Name %></A></TD>
                  <TD ALIGN="right"><%= size %></TD>
                  <TD ALIGN="left" ><%= dateCreated %></TD>
                  <TD ALIGN="left" ><%= objItem.Type %></TD>
            </TR>
            <%
      End If
Next 'objItem

' Now that I've done the SubFolders, do the files!
For Each objItem In objFolder.Files
if objItem.Name = "default.asp" or objItem.Name = "docList.asp" then
 
else

      size = objItem.Size
      dateCreated = objItem.DateCreated
      objType = objItem.Type

      intEndPos = Instr(dateCreated, " ")
      dateCreated = Mid(dateCreated, 1,intEndPos)
      
      t=" b"
      if size>1024 then
            size = round(size/1024,0)
            t="Kb"
            if size>1024 then
                  size = round(size/1024,0)
                  t="Mb"
            end if
      end if
      size = size & t
%>
      <TR >
            <TD ALIGN="left" ><A HREF="<%= strPath & objItem.Name %>" target="_new window" ><%= objItem.Name %></A></TD>
            <TD ALIGN="right"><%= size %></TD>
            <TD ALIGN="left" ><%= dateCreated %></TD>
            <TD ALIGN="left" ><%= objType %></TD>
      </TR>
<%
end if
Next 'objItem

' All done!  Kill off our object variables.
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

' Oops... I almost forgot to close up the table.  IE will forgive you,
' but Netscape will catch you on this error - AS IT SHOULD!

' Ok, so I'm a little odd being a Microsoft supporter and yet I still
' use Netscapes's browser... I just like it better... so sue me!
'
' I probably shouldn't say that too loudly...
'                    ...you never know what the next lawsuit will be!
%>
</TABLE>
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 9350254
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp


Cd&

0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question