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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Images from a directory

Status
Not open for further replies.

asfaw

Programmer
Jun 28, 2004
36
CA
Is there a way (JavaScript) to load (offscreen images and caching) images in a particular directory on the webserver. The directory could be "images/". I would like to do this without specifying the file name. I would like to do this using Javascript

Your help is very much appreciated.

asfaw
 
Without specifying the filenames, you will NOT be able to do this client-side... how will the browser know what to load otherwise?

You will have to either deliver the filenames from the folder to the client each time the script is delivered, or hard-code the filenames into the script.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If you can support server side javascript you could use the FileSystemObject to return a list of files in the folder and even filter them by type in case there were file types not for display in your app.

As Dan said though, you cannot do it client-side.

If you support ASP I have some code that will read a specified folder and return all the filenames and you could use that to pass the filenames into your image pre-loader.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Thank you all. All the files are in one folder - if I were to specify the directory where the files are may I be able to put all the files an an array? i.e. all the files are on the server. When the client goes to the specified page I want the browser to access the files on the server and put them in an array.

asfaw

 
I do not think it can be done with client side script.
You could possibly read a file on the server containing the names but not read the folder itself to see what files are in it.

You need server side script to read the filenames and write them into an array.

Does your server support ASP?
I have code that uses the FileSystemObject to read the filenames from a specified folder and it could be easily modified to output a javascript array of the names.


Paranoid? ME?? WHO WANTS TO KNOW????
 
If your server supports ASP this this should work for you.
Code:
<html>
<head>
<%
  Dim fso
  Set fso = Server.CreateObject("Scripting.FileSystemObject")
  Dim rootFolder
  Set rootFolder = fso.GetFolder(Server.MapPath("/mywebfolder/images/"))
  Dim files
  Set files = rootFolder.Files

  ' Write the javascript header
  response.write "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
  ' then declare the array
  response.write "var arrImages = new Array();" & vbCrlf
  outstr="arrImages = ["

  For Each file in files
    If file.Type = "GIF Image" OR file.type = "JPG Image" Then
      ' Store filename in Client Side Javascript array arrImages
      If cnt <> 0 then
        outstr = outstr & ","
      End if
      cnt=cnt+1
      outstr = outstr & "'" & file.Name & "'"
    End If
  Next

  outstr = outstr & "];" & vbCrLf
  response.write outstr
  ' write the script trailer...
  Response.Write "</SCR" & "IPT>" & vbCrlf

  Set files = Nothing
  Set rootFolder = Nothing
  Set fso = Nothing
%>

<script language="javascript">
for (var loop=0;loop<arrImages.length;loop++)
{
  document.write(arrImages[loop] + "<br>");
}
</script>
</head>
<body>
</body>
</html>

If your server supports server-side javascript the above function can be easily re-written as javascript.
But the function has to operate server side in order to use the FileSystemObject to read the filenames in the specified folder.

If it does not work then the IUSR_machinename account may have to be given permissions to allow it to use the FileSystemObject.

Also, remember that the way the path is set will point it to your not to your Scripts folder so your images folder should be under the

Paranoid? ME?? WHO WANTS TO KNOW????
 
Thank you for your kind help. I know it can be done using Serverside scripting e.g. PHP. I was wondering where it can be done using client side JavaScript. I wll just use ServerSide.

Many thanks and best regards.

asfaw
 
I could be wrong but I think that using activex to get to filesystemobject is only going to give you access to files on the local computer, not the server.

It is possible to use this method to go to a network path but that would occur via the clients network connection under the clients permissions.

To get to server side filesystemobject you need server side code and that operates under the web servers IUSR_machinename account.

It is possible to have a server side text file of filenames that you can retrieve with javascript and read into an array. You would still need server side code to automate creation of the text file though.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Many thanks. I have used PHP to do it.

asfaw
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top