如何使用jquery获取目录中的文件计数? [英] How to get the count of file in a directory using jquery?

查看:774
本文介绍了如何使用jquery获取目录中的文件计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得文件夹中的文件数量。在我的文件夹中有很多图像我想要数数有没有任何方法可以使用jquery或javascript?
提前致谢

i want t get the number of files in a folder. there are many images in my folder i want to count there numbers is there any way to do that using jquery or javascript ? Thanks in advance

推荐答案

如果服务器是Apache且允许目录列表,那么你就是AJAX'目录,如果没有索引文件,Apache通常会返回一个包含 c>< table> 的文档,其中包含目录中的文件。您可以从文档中提取< table> 并显示它,或计算包含文件信息的表中的行数。我会认为该表中的任何行都有一个值为0或更高的大小列作为文件。

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.

标记:

<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');
    }
});

这篇关于如何使用jquery获取目录中的文件计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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