简体   繁体   中英

How to create an array from files in a folder?

arr01 =[ "01.png", "02.png", "03.png", "04.png", "05.png"...];
arr02 =[ "01.png", "02.png", "03.png", "04.png"...];
arr03 =[ "01.png", "02.png", "03.png", "04.png"...];

These images are files in folders named 01, 02, 03...

I need to create the arrays to managing images in a slider.

Is this possible telling: give me all file names in folder 01 , and create an array from them ?

you can't do this with client-side code language. You should do this server-side and if you want, return an array that will be handled with javascript afterwards

It needs to be done server-side, but I'd suggest writing a small service that returns you a JSON structure that is representative of what you want to display. It's not that hard to write a recursive algorithm that'll convert a directory tree into a JSON tree. The advantage is that instead of having arr01 , arr02, , etc. you'll have something like this:

{
  root: '/media',
  items: [
    'image.png',
    'image2.png',
    'vacation': [
       'image3.png',
       //You get the idea
    ]
  ]
}

That'll be a lot easier for you to work with on the client side that something which generates a lot of different variables. Use the server-side to help you do code generation only to the point of JSON.

Edit : another reason to use JSON and AJAX here is that using something like JSP to generate JavaScript takes time that could be better spent showing the user some sort of loading message while the server fetches some additional data. You'll find this especially true of a lot of "enterprise apps" which are heavy on generating the HTML's full contents on the server-side. By having the service layer handle tasks like database lookups and subsequent conversion of data into JSON, you'll save more server side resources than doing all of the HTML generation on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM