简体   繁体   中英

How to show file content in html?

I want to make my own To-Do list for that I need to display the content of a file. The problem is that i don't want to select the file and want the content to be displayed directly. Can anyone help me? (I want to display the content of a.txt file on a html side without selecting the file(so just in the source code))

You can use FileReader for reading the content of the text file:

 document.getElementById('inputfile').addEventListener('change', function() { var fr=new FileReader(); fr.onload=function(){ document.getElementById('output').textContent=fr.result; } fr.readAsText(this.files[0]); })
 <input type="file" name="inputfile" id="inputfile"> <br> <pre id="output"></pre>

Source: https://www.geeksforgeeks.org/how-to-read-a-local-text-file-using-javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM