javascript 如何使用jquery获取目录中的文件数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29232134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the count of file in a directory using jquery?
提问by user2142786
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
我想获取文件夹中的文件数。我的文件夹中有很多图像我想计算这些数字有没有办法使用 jquery 或 javascript 来做到这一点?提前致谢
回答by davidkonrad
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.
如果服务器是 Apache 并且允许目录列表,并且您 AJAX '一个目录,如果没有索引文件存在,Apache 通常会返回一个<table>
包含目录中文件的文档。您可以<table>
从文档中提取并显示它,或者计算包含文件信息的表中的行数。我会考虑该表中具有值为 0 或更高值的大小列的任何行作为文件。
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');
}
});
回答by Utku Cansever
I didn't tried yet, but could be like this.
我还没有尝试过,但可能是这样的。
$.ajax({
url: "/images-folder-on-server/",
success: function (data) {
var image_count = $(data).length();
}
});
回答by Surajghosi
JavaScript is client side language. it not allowed to access file to server, if you are using ASP.net then use below line of code for get count of file.
JavaScript 是客户端语言。它不允许访问文件到服务器,如果您使用的是 ASP.net,则使用下面的代码行获取文件计数。
IO.Directory.GetFiles("\translated\path","27_*.jpg").Count()