Which Javascript I need for a carousel with random photos from specific folder?

前端 未结 2 1527
天命终不由人
天命终不由人 2021-01-26 02:27

I have a problem that seems small, but it is no longer making me sleep at night, I am probably thinking about it too much and I have lost sight of the correct path!

I\'m

2条回答
  •  不要未来只要你来
    2021-01-26 03:01

    Unfortunately, reading data from a directory isn't supported in JavaScript. You might need a server side language like PHP or Node JS to read the directory and add them in HTML.

    For example, if it's PHP, you can do something like List all files in one directory PHP. You can use that array to build your array:

    '; }

    Or if you're using Node JS, it's still simpler:

    const directoryPath = path.join(__dirname, 'Documents');
    //passsing directoryPath and callback function
    fs.readdir(directoryPath, function (err, files) {
        //handling error
        if (err) {
            return console.log('Unable to scan directory: ' + err);
        } 
        //listing all files using forEach
        files.forEach(function (file) {
            // Do whatever you want to do with the file
            console.log(''); 
        });
    });
    

    Ultimately you need a server side language for this.

提交回复
热议问题