java 打印txt_如何使用java腳本在文件夾中打印所有txt文件?

这篇博客介绍了如何使用JavaScript结合HTML的input[type=file]元素读取并打印txt文件,包括通过FileReader API逐个读取文件以及处理目录上传。在Chrome、Chromium和Firefox中,利用不同的API和配置实现本地文件系统的访问。示例代码提供了详细的实现过程。
摘要由CSDN通过智能技术生成

3

You can use with multiple attribute set, accept attribute set to text/plain; change event ;FileReader, for loop.

可以使用與多個屬性集,接受屬性集為text/plain;更改事件;FileReader, for循環。

var pre = document.querySelector("pre");

document.querySelector("input[type=file]")

.addEventListener("change", function(event) {

var files = event.target.files;

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

(function(file) {

var reader = new FileReader();

reader.addEventListener("load", function(e) {

pre.textContent += "\n" + e.target.result;

});

reader.readAsText(file)

}(files[i]))

}

})

 
 

You can also use webkitdirectory and allowdirs attributes for directory upload at chrome, chromium; at nightly 45+ or firefox 42+ where dom.input.dirpicker preference set to true, see Firefox 42 for developers , Select & Drop Files and/or Folders to be parsed. Note, you can also drop folders from file manager at element

你也可以使用webkitdirectory和allowdirs屬性的目錄上傳在chrome, chromium;在每晚45+或firefox42 +的位置輸入。dirpicker首選項設置為true,請參閱Firefox 42,選擇並刪除要解析的文件和/或文件夾。注意,您還可以在元素下拉文件夾

var pre = document.querySelector("pre");

document.querySelector("input[type=file]")

.addEventListener("change", function(event) {

console.log(event.target.files)

var uploadFile = function(file, path) {

// handle file uploading

console.log(file, path);

var reader = new FileReader();

reader.addEventListener("load", function(e) {

pre.textContent += "\n" + e.target.result;

});

reader.readAsText(file)

};

var iterateFilesAndDirs = function(filesAndDirs, path) {

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

if (typeof filesAndDirs[i].getFilesAndDirectories === 'function') {

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]))

}

}

})

 
 

plnkr http://plnkr.co/edit/Y1XYd9rLOdKRHw6tb1Sh?p=preview

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?.

對於chromium上的ajax請求,chrome file:協議本地文件系統,您可以使用—allow-file-access-from-files標志進行啟動,請參見僅在firefox中工作的Jquery load()。

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

在firefox中,可以設置security.fileuri。strict_origin_policy為false,請參見Security.fileuri。嚴格的政策。

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

對於chrome的一種可能的$.ajax()方法,可以嘗試chromium

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)

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值