[英]Reading a file line by line in Javascript and HTML
因此,我想創建一個網站,該網站將回答一個問題或每行上載一個問題的.txt文件,然后閱讀該文件,然后使其僅使用javascript(不是jquery或node.js)和HTML5進行下載:
而不是輸入文件
我將完全不知所措。 到目前為止,這是我的代碼:
<!DOCTYPE html>
<html>
<head>
<title>Random Facts</title>
</head>
<body>
<h1 align="center">Random Facts & Stuff</h1>
<p>To ask a question, simply ask it through the input bar below, or, if
you want, you can upload a file and then hit download, then it will
download a file.txt with the questions you put in and then the answers
after the colin:</p>
<br>
<p>Ask a question here</p>
<input type="text" id="question1" />
<br>
<p>Or upload a file here</p>
<input type="file" id="question2" />
<br>
<br>
<button type="button" id="go">Go!</button>
<script>
// Do this for when the button is clicked
document.getElementById('go').onclick = function() {
// Check whether or not the first input field is blank
if (document.getElementById('question1').value.length == 0) {
// Do this if the first text field is empty, aka, read the file.
// create the file reader.
var reader = new FileReader();
} else {
// If the text field is not empty, do this.
alert("Text");
}
}
var reader = new FileReader();
</script>
</body>
</html>`
沒有Node.js,您將無法使用文件系統,這就是為什么您不加載.txt文件而僅使用“回答問題”的原因。
如果要在第一個問題之后顯示第二個問題,則可以:1)使用回調2)閱讀有關生成器(es6)或使用Promises(或await函數)的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.