Load Random Image From Directory on Page Load Without a Listed Array of File Names

前端 未结 4 1026
一向
一向 2021-01-26 21:32

I\'ve done some looking around on the site and every time I pull up a solution to this problem, one of the requirements is to have a naming convention and a list of every image

4条回答
  •  Happy的楠姐
    2021-01-26 22:11

    One of the main problems your facing here is about your thinking when it comes to how content is delivered, in a standalone static website you do not have access to the file system. This means that if we want to query things outside of the browsers context we are not allowed, obviously without being able to access directories we can not generate a list of file names which can be loaded.

    If your wondering why we can't access the file system directly from say the JavaScript it's because of the sandbox that most modern browsers live in, otherwise people could attack your native directories from the front end languages. Your question is interesting as electron removes this sandboxing in a sophisticated esk manner, which is necessary as it's used for building desktop apps with chromium.

    These days the most obvious solution would be to use some form of back end language and to create a web server that has direct access to the native directories around it. Node, PhP, GoLang and many other populatr backend languages can parse a directory of files and then interpolate those into the frontend code which is the most common method.

    The other popular method at the moment is to create API's which is just a fancy web server with a queryable end point that then executes code against our web server and provides back a list of such items. You could then for instance take the items and then print those out using javascript.

    Reference directories method in php: http://php.net/manual/en/ref.dir.php

    List contents of directory in nodejs: https://code-maven.com/list-content-of-directory-with-nodejs

    The best place to really start with the easiest route to understand more would be to start a backend language in either node or php, with php being the simpler of the two.

    https://www.w3schools.com/php/

提交回复
热议问题