Bha*_*kar 10 html javascript file-upload
有没有办法列出网页中目录的文件,并带有查看文件的链接.
我想将一些PDF文件上传到我的静态网站(html + js)的某个目录,并希望在页面中显示文件列表,其中包含查看pdf文件的链接,这样如果我上传新文件,我就不要每次都要修改html页面,有没有办法做到这一点?
如果您使用 Apache 作为网络服务器,并为您上传 pdf 文件的目录配置了mod-autoindex,那么您应该在导航到该 url 时看到类似这样的想法:

这个自动生成的页面可以很容易地被 js 使用 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
})
});
Run Code Online (Sandbox Code Playgroud)