If the server is Apache and Directory Listing is allowed, and you AJAX' a directory, Apache will typically return a document with a <table>
containing the files in the directory if there is no index files present. You can extract the <table>
from the document and show it, or count the number of rows in the table containing file information. I would consider any row in that table having a size column with value 0 or higher as a file.
markup :
<div id="fileCount"></div>
<div id="files"></div>
ajax:
$.ajax({
url: "images/",
success: function(data) {
var parser = new DOMParser(),
doc = parser.parseFromString(data, 'text/html');
//output the file table
$("#files").append(doc.querySelector('table').outerHTML);
//or return the number of files
//tr = icon, filename, date, size, desc
//consider all rows with a size value holding a number as a vlid file
var fileCount = 0,
rows = doc.querySelector('table').querySelectorAll('tr');
for (var i=0;i<rows.length;i++) {
if (rows[i].children[3]) {
if (parseInt(rows[i].children[3].innerText)>0) fileCount++;
}
}
$("#fileCount").text(fileCount+' files');
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…