让我们考虑一个自定义扩展名.cst,并且我们有一个专门设计用于读取.cst文件的javascript函数CSTReader()。这是一个例子:
files.cst:
I am a cst file. I have data in me such that I am readable by javascript.
index.js:
document.write("Reqd Output : " + CSTReader("files.cst") );
输出:
Reqd Output : I am a cst file. I have data in me such that I am readable by javascript.
那么,可以制作CSTReader()吗?还是在javascript中是不可能的?如果没有,还有其他选择吗?还是只是浪费时间?
提前致谢
您可以使用FileReader
object来这样做。它也将适用于自定义扩展。查看https://developer.mozilla.org/fr/docs/Web/API/FileReader以获取文档。
如果您的文件包含纯文本,则可以使用该readAsText
方法。
在您的示例中,您可以执行以下操作:
<input type="file" onchange="readFile"/> // put your file here
<script lang="js">
function readFile(e) {
let file = e.target.files[0] // get the file
let fileReader = new FileReader(); // instanciate Filereader
fileReader.onloadend = function () { // bind your function to the onloadend method. It will be executed once the file is read.
console.log(filereader.result);
// do whatever you want with the content
};
fileReader.readAsText(file); // read the file, whatever the extension is.
}
</script>
您也可以看一下这个演示。
祝好运
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句