HTML脚本标记内的JavaScript代码不工作
我想访问特定路径中的文件名。单独编写的JavaScript代码可以很好地工作,而当它放在HTML中时,代码就不工作了HTML脚本标记内的JavaScript代码不工作,javascript,html,Javascript,Html,我想访问特定路径中的文件名。单独编写的JavaScript代码可以很好地工作,而当它放在HTML中时,代码就不工作了 fs = require('fs'); var directory = 'IPIE/';//direcory name(path name) fs.readdir(directory, (err, files)=> { for (index in files) { console.log(files[index]); } }); 此代码给出输出,
fs = require('fs');
var directory = 'IPIE/';//direcory name(path name)
fs.readdir(directory, (err, files)=> {
for (index in files) {
console.log(files[index]);
}
});
此代码给出输出,即文件夹名称,如下所示:
BigBubbles
Edgegap
Sticky
然而,下面的html代码并不能很好地工作
<html>
<body>
<label>Input Option:</label>
<select id="input_mode"></select>
<button onclick='displayfiles()'>abc</button>
</body>
<script>
function displayfiles() {
var fs = require('fs');
var directory = 'IPIE/'; //folder name
fs.readdir(directory, (err, files) => {
var select = document.getElementById("input_mode");
for (index in files) {
select.options[select.options.length] = new Option(files[index], index);
}
});
}
</script>
</html>
输入选项:
abc
函数displayfiles(){
var fs=需要('fs');
var目录='IPIE/';//文件夹名称
fs.readdir(目录,(错误,文件)=>{
var select=document.getElementById(“输入模式”);
用于(文件中的索引){
select.options[select.options.length]=新选项(文件[index],索引);
}
});
}
我注意到在var fs=require('fs')之后代码>行,警报(“某物”)
不起作用,这意味着执行将在该行停止。。???
请帮助为了输出文件列表,您必须设置一个web服务器,在服务器端获取文件列表,然后输出到浏览器。执行和访问
var http=require('http');
var fs=需要('fs');
http.createServer(函数(req,res){
文书标题(200{
“内容类型”:“文本/html”
});
var fileSelectHtml=getFileSelectHtml();
res.end(`
输入选项:
${fileSelectHtml}
`);
}).听(80);
函数getFileSelectHtml(){
var files=fs.readdirSync('IPIE/');
var selectHtml='';
用于(文件中的索引){
选择HTML+=''+文件[索引]+'';
}
选择HTML+='';
返回selectHtml;
}
'fs'是一个Node.js模块,在浏览器上不工作是正常的。@yip102011哦!谢谢是否有其他通过html访问文件名的过程?同样require
在浏览器上不起作用。接下来,不允许浏览器访问文件系统(谢天谢地!!)。
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
var fileSelectHtml = getFileSelectHtml();
res.end(`
<html>
<body>
<label>Input Option:</label>
${fileSelectHtml}
</body>
</html>
`);
}).listen(80);
function getFileSelectHtml() {
var files = fs.readdirSync('IPIE/');
var selectHtml = '<select id="input_mode">';
for (index in files) {
selectHtml += '<option>'+ files[index] +'</option>';
}
selectHtml += '</select>';
return selectHtml;
}