How do I get a random file from a folder?


I have a folder under www named “random” containing files and I need to be able to call a function that retrieves the names of X number of files.

I assume that I could put the list of files in an array and randomly select one from the array. But I cannot figure out how to get the list of files.

Thank you!

Its not possible with plain javascript

If you building an app use the cordova file plugin available via ngCordova

See for further informations

Not sure if this helps

Alternatively, if the list of files doesn’t change, you could generate the list in your build process using your preferred method of listing directory contents.

The list doesn’t change so I created an array of the file names and now, therefore, I can get random file names. However, I cannot figure out how to get the file by relative path.

I tried “/random/file.txt” which resolved to
which is the correct path but I get 404.

What is the correct, safe, preferred method for pathing to a resource?

Thank you!