Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying images in a directory using javascript 1

Status
Not open for further replies.

6930

Programmer
Dec 3, 2005
4
DE
Hi
I need to display all the images in a directory(located on server). How can I do that using Javascript? Please help. Thanx in advance.
 
If you need to display all images on a server, why would you want to use a client side language (in this case, javascript) to do it?

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
images
 
If you want to do it server side I have a nice bit of code that will read all files in a specified folder and filter for specified image types. It returns the name of the file so you can dynamically build your links or pass the names into a script to pre-load the images.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Thanks a lot theniteowl. Can you please share the code with me. Thanks again.
 
This could possibly be re-written for javascript but would have to be run server side in order to create the FileSystemObject to get at the server folder.

This code does not have to point at a web server folder for the images, it could look at a server folder elsewhere on the network but the IUSR_machinname account would have to have access to that network folder and the methods of authentication be matched between the file server and the IIS server.

The commented lines are there to show what other file attributes you can easily get at.

The If statement that filters for GIF and JPG file types can be modified to include other types as well, this is just what I put in for testing.

Essentially the code just looks at the file folder and loops through each file in the folder testing to see if it's file extension matches one in the IF statement and if it does it spits out the name on the screen. You can instead insert the name into an array to be used later, immediately generate the <IMG tag with that path/filename for it's source, use an image pre-load script to retrieve the image into memory for future use, etc.

The main point of this code is to allow you to read ALL images in a folder of specified types without having to know the filenames ahead of time and eliminate the need to hard-code the names into your script or write them into some sort of control file.


Code:
<%
  Dim fso
  Set fso = Server.CreateObject("Scripting.FileSystemObject")
  Dim rootFolder
  Set rootFolder = fso.GetFolder("\\servername\inetpub\[URL unfurl="true"]wwwroot\myfolder\images\")[/URL]
  Dim files
  Set files = rootFolder.Files
  For Each file in files
    If file.Type <> "GIF Image" AND file.type <> "JPG Image" Then
      Response.Write "FileName: " & file.Name & "<br>"
    End If
Response.Write UCase(file.Type)
'	Response.Write "Attributes: " & file.Attributes & "<br>"
'	Response.Write "Type: " & file.Type & "<br>"
'	Response.Write "Size: " & file.Size & "<br>"
'	Response.Write "ShortName: " & file.ShortName & "<br>"
  Next
  Set files = Nothing
  Set rootFolder = Nothing
  Set fso = Nothing
%>

Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top