列出静态网页中目录的文件 [英] Listing files of a directory in a static webpage

查看:146
本文介绍了列出静态网页中目录的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在静态网页中列出目录文件并提供查看文件的链接?

Is there any way to list the files of a directory in a static webpage with the link to view the file?

我想将一些PDF文件上传到我的静态网站的某个目录(它使用HTML和JS),并希望在页面中显示文件列表以及查看pdf文件的链接.这样,如果我上传新文件,则不必每次都修改HTML页面.有什么办法吗?

I would like to upload some PDF files to a certain directory of my static website (it uses HTML and JS), and want to show the list of the files in a page with the link to view the pdf files. That way, if I upload new files, I don't have to modify the HTML page every time. Is there any way to do that?

推荐答案

如果您将Apache用作网络服务器并已配置

If you are using Apache as a web-server and have configured mod-autoindex for the directory you upload pdf files then you should probalby see somethink like this when navigation to that url:

js可以使用jquery轻松解析此自动生成的页面:

This auto-generated page can be easily parsed by js using jquery:

var pdfFilesDirectory = '/uploads/pdf/';

// get auto-generated page 
$.ajax({url: pdfFilesDirectory}).then(function(html) {
    // create temporary DOM element
    var document = $(html);

    // find all links ending with .pdf 
    document.find('a[href$=.pdf]').each(function() {
        var pdfName = $(this).text();
        var pdfUrl = $(this).attr('href');

        // do what you want here 
    })
});

这篇关于列出静态网页中目录的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆