[英]How to open a local disk file with JavaScript and check whether it has the word which we needed?
我想用JavaScript打開文本或Word文件。 之后,我希望程序掃描它是否包含我需要的單詞。 如果它有世界,那么它應該在找到單詞時發出警報,而在沒有單詞時發出警報。 請幫助我。 我寫了一個從文件讀取的代碼,但是我不確定文件的內容存儲在哪里以及在哪里使用搜索功能。
我當前的代碼是
<h1>File reader</h1>
<div>
Select a text file:
<input type="file" id="fileInput">
<pre id="displayArea"><pre>
</div>
window.onload = function() {
var fileInput = document.getElementById('fileInput');
var displayArea= document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function(e) {
displayArea.innerText = reader.result;
}
reader.readAsText(file);
} else {
displayArea.innerText = "File not supported!"
}
});
}
通過使用此選項,我可以在輸出中獲取文件內容,但是我需要輸出作為文件是否包含所需單詞的警報。
您對結果不采取任何措施,這就是為什么必須將reader.result
輸出存儲在變量中,然后查看您要查找的單詞是否在該輸出中並顯示警報。
像這樣
var readResult = reader.result;
if (/Hello/.test(readResult)) {
alert("The word exist");
} else {
alert("The word does not exist");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.