简体   繁体   English

为文件夹中的每个文件制作一个下载按钮

[英]Make a download button for each file in a folder

I have a website that host some files on it, they are all in a download folder.我有一个网站,上面有一些文件,它们都在一个download文件夹中。 I would like to know, if its possible to make an automation;我想知道,是否可以进行自动化; each file have its dedicated button, and each button is like this file.exe download .每个文件都有其专用的按钮,每个按钮就像这个file.exe download Or I have to manually type every button ?或者我必须手动输入每个按钮? Thanks!谢谢!

You can use .forEach() in Javascript to repeat making a button for each file.您可以在 Javascript 中使用 .forEach() 为每个文件重复制作一个按钮。

Javascript Javascript

var files = []; //array containing the file paths, e.g. "/download/file.exe"
files.forEach(file => {
    document.write(`<a href="${file}" download>
                        <button>Download ${file}</button>
                    </a>`)
})

If you want to make the button say "Download What you call the file " instead, you can make a separate array with the names:如果您想让按钮显示“下载您调用的文件”,您可以使用名称创建一个单独的数组:

var files = []; //array containing the file paths, e.g. "/download/file.exe"

var filenames = []; //array containing the file names, e.g. "File.exe"

files.forEach((file, index) => {
    document.write(`<a href="${file}" download>
                        <button>Download ${filenames[index]}</button>
                    </a>`)
})

if you build web from php js you can simple put script foreach assume that you have array all file name in variabel如果您从 php js 构建网络,您可以简单地放置脚本 foreach 假设您在变量中拥有数组所有文件名

$data = array('download1.pdf', 'download2.pdf'); // example your array file 
<?php  $link = 'example.com/download'; ?> // your path download
<?php foreach($data as $download) { ?>
 <a href="<?= $link."/".$download ?>" _target='blank'?><?= $download ?></a>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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