Jquery 如何从给定文件夹中检索文件名
在这里,我试图从给定目录中获取所有文件名的列表 我正在尝试从扩展名为“.txt”的“/usr/local”目录检索所有文件 由于以下错误,我无法显示文件名: 已阻止跨源请求:同一源策略不允许读取位于的远程资源。(原因:CORS标题“访问控制允许来源”丢失)。我如何克服此问题。是否有人可以帮助我解决此问题 My Sample.html:Jquery 如何从给定文件夹中检索文件名,jquery,html,ajax,node.js,Jquery,Html,Ajax,Node.js,在这里,我试图从给定目录中获取所有文件名的列表 我正在尝试从扩展名为“.txt”的“/usr/local”目录检索所有文件 由于以下错误,我无法显示文件名: 已阻止跨源请求:同一源策略不允许读取位于的远程资源。(原因:CORS标题“访问控制允许来源”丢失)。我如何克服此问题。是否有人可以帮助我解决此问题 My Sample.html: <html> <script src="https://code.jquery.com/jquery-1.10.2.js"></sc
<html>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<body>
<div id="fileNames"></div>
<script type="text/javascript">
$(window).load(function(){
var fileExt = ".txt";
$(document).ready(function(){
$.ajax({
url: 'http://localhost:3000/dir',
success: function (data) {
console.log(data);
$("#fileNames").html('<ul>');
$(data).find("a:contains(" + fileExt + ")").each(function () {
$("#fileNames").append( '<li>'+$(this).text()+'</li>');
});
$("#fileNames").append('</ul>');
}
});
});
});
</script>
</body>
</html>
$(窗口)。加载(函数(){
var fileExt=“.txt”;
$(文档).ready(函数(){
$.ajax({
网址:'http://localhost:3000/dir',
成功:功能(数据){
控制台日志(数据);
$(“#文件名”).html(“”);
$(数据)。查找(“a:包含(+fileExt+))。每个(函数(){
$(“#文件名”).append('- '+$(this.text()+'
');
});
$(“#文件名”).append(“
”);
}
});
});
});
关于在目录中列出
关于错误-尝试在终端curl中执行http://localhost:3000/dir
有两种方法-服务器错误或客户端错误。原始答案(2017):
** Firstly we will have to upload files using FTP on accessible location then using this code we can get the files names. #In my case i am redirecting to the file location.
$(document).ready(function() {
GetDirectoryFiles(); //calling of function
});
function GetDirectoryFiles() { //function Definition for Get All FileNames Of Directory
$.ajax({
type: "GET",
url: "http://localhost:55304/TestViewsGoogleImgChanged/",
contentType: "application/html; charset=utf-8",
data: null,
dataType: "html",
success: function(data) {
var pagedata = $.parseHTML(data);
var tabledata = $.parseHTML(pagedata[9].innerHTML);
for (var i = 0; tabledata.length / 2; i++) {
if (tabledata[i].textContent.indexOf('.cshtml') > 0) {
window.open('http://123.com/' + tabledata[i].textContent.split(".cshtml")[0], '_blank')
}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + '-----' + errorThrown);
}
});
}
var fs = require('fs');
var files = fs.readdirSync('/usr/local/');
var fs = require('fs');
//Gets all files with .txt extension at the /usr/local/ directory:
const files = fs.readdirSync("/usr/local/").filter(file => file.endsWith(".txt"));
//Loops through all of the files to do something with each one:
for (var file of files){
//Do something with each filename... (var file contains the filename)
}
当我最初回答这个问题时,我不知道如何使用nodeJS。我只是从以下问题中得到了答案,并为您修改了它:
不过,现在我对nodeJS有了更多的经验,可以正确地回答这个问题。如果您只需要以.txt扩展名结尾的文件,可以执行以下操作
更新答案(2020):
var fs = require('fs');
var files = fs.readdirSync('/usr/local/');
var fs = require('fs');
//Gets all files with .txt extension at the /usr/local/ directory:
const files = fs.readdirSync("/usr/local/").filter(file => file.endsWith(".txt"));
//Loops through all of the files to do something with each one:
for (var file of files){
//Do something with each filename... (var file contains the filename)
}
我以前使用过这种方法,我可以保证它是有效的,假设我在任何地方都没有打字错误。希望这有帮助
请注意,这必须在服务器端完成,而不是客户端,因为这是NodeJS代码。您的站点托管在哪里?ie你输入什么url来打开这个页面?我猜这不是
http://localhost:3000/index.html
这是打开此页面的url OK-那么是什么服务于对/dir
的请求呢?IIS/Apache?您使用的服务器端技术是什么?php/asp.net?如果直接在浏览器中输入/dir
(而不是通过ajax请求),您会得到什么?除了html之外,您还可以控制其他内容吗?我使用node.js作为服务器端技术。如果我输入,我将在浏览器中获得“/usr/local/”目录名。这有助于:注意,尽管这会获得目录中的所有文件,但它不会按照OP的要求专门过滤掉.txt文件。读取当前目录(包括保存此脚本的文件)可能会有问题,特别是如果脚本的功能是对目录中的所有JS文件执行require()
。您可能会陷入一个无限循环,这个JS文件需要自己。