java 打印txt,如何使用java脚本打印文件夹中的所有txt文件

I need to print all the txt files from a directory inside an HTML using javascript.

i tried to modify a code dealing with photos but I didn't success

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("

var path = filesAndDirs[i].path;

// this recursion enables deep traversal of directories

filesAndDirs[i].getFilesAndDirectories()

.then(function(subFilesAndDirs) {

// iterate through files and directories in sub-directory

iterateFilesAndDirs(subFilesAndDirs, path);

});

} else {

uploadFile(filesAndDirs[i], path);

}

}

};

if ("getFilesAndDirectories" in event.target) {

event.target.getFilesAndDirectories()

.then(function(filesAndDirs) {

iterateFilesAndDirs(filesAndDirs, '/');

})

} else {

// do webkit stuff

var files = event.target.files;

for (var i = 0; i < files.length; i++) {

(function(file) {

uploadFile(file)

}(files[i]))

}

}

})

 
 

For ajax requests at chromium, chrome file: protocol local filesystem you can launch with --allow-file-access-from-files flag set, see Jquery load() only working in firefox?.

At firefox you can set security.fileuri.strict_origin_policy to false, see Security.fileuri.strict origin policy.

For a possible $.ajax() approach at chrome, chromium you can try

var path = "/path/to/drectory"; // `D:\`, `file:///`

var files = [];

$.ajax({url:path, dataType:"text html"})

.then((data) => {

// match file names from `html` returned by chrome, chromium

// for directory listing of `D:\Finaltests\test1\new\places`;

// you can alternatively load the "Index of" document and retrieve

// `.textContent` from `` elements within `td` at `table` of

// rendered `html`; note, `RegExp` to match file names

// could probably be improved, does not match space characters in file names

var urls = $.unique(data.match(/\b(\w+|\d+)\.txt\b/g));

return $.when.apply($, $.map(urls, (file) => {

files.push(file);

// `\`, or `/`, depending on filesystem type

return $.ajax({url:path + "/" + file

, dataType:"text html"})

.then((data) => {

// return array of objects having property set to `file` name,

// value set to text within `file`

return {[file]:data}

})

}))

})

.then((...res) => {

console.log(res, files)

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值