1 回答

TA贡献1862条经验 获得超7个赞
您可以使用FileReader对象来执行此操作。它也适用于自定义扩展。查看https://developer.mozilla.org/fr/docs/Web/API/FileReader以获取文档。
如果您的文件包含纯文本,则可以使用该readAsText方法。
在您的示例中,您可以执行以下操作:
<input type="file" onchange="readFile"/> // put your file here
<script>
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>
添加回答
举报