如何使用Java脚本打印文件夹内的所有txt文件我需要使用javascript从HTML内的目录中打印所有txt文件。我试图修改处理照片的代码,但未成功如何使用Jquery / Javascript将文件夹中的所有图像加载到网页中var dir = "D:\Finaltests\test1\new\places";var fileextension = ".txt";$.ajax({
//This will retrieve the contents of the folder if the folder is configured as 'browsable'
url: dir,
success: function (data) {
//List all .txt file names in the page
$(data).find("a:contains(" + fileextension + ")").each(function () {
var filename = this.href.replace(window.location.host, "").replace("http://", "");
$("body").append("<input type='file' onload='readText(" + dir + ")>");
});
}});
3 回答
慕田峪4524236
TA贡献1875条经验 获得超5个赞
出于安全原因,您无法使用javascript访问主机文件系统。最好的办法是对服务器端脚本(例如php)进行单个ajax调用,该脚本将收集所有html文件并将其返回给您的ajax调用。
gethtml.php:
<?php
$output = "";// your list of folders$folders = [
'D:\Finaltests\test1\new\places1',
'D:\Finaltests\test1\old\places2',
'D:\Finaltests\test1\whatever\places3'];foreach ($folders as $key => $dir) {
if(!is_dir($dir))
continue;
// get all files of the directory
$files = scandir($dir);
foreach ($files as $file) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if(finfo_file($finfo, $file) == 'text/plain')
$output .= file_get_contents($dir . DIRECTORY_SEPARATOR . $file);
}}echo $output;exit;
?>Ajax呼叫:
$.get('path/to/gethtml.php', function(response){
$('body').html(response);});您可以根据要获取的文件的类型(纯文本或text / html或其他内容)来更改检查mime类型的php行
慕丝7291255
TA贡献1859条经验 获得超6个赞
您在differents目录中有HTML页面,并且要将它们全部打印在一页中吗?如果是的话,您应该编写一个小的服务器端脚本来浏览每个目录,并将每个目录的内容连接到一个变量中,该变量将在完成时打印,以便您只能进行1个ajax调用并一次获取所有html
添加回答
举报
0/150
提交
取消
