Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  44] [ 1]  / answers: 1 / hits: 25496  / 9 Years ago, tue, november 10, 2015, 12:00:00

I have a <textarea> element and a button that calls a loadFile() JavaScript function. I want this function to allow me to load the text from a .txt file on my local machine into the textarea. Any help with this would be greatly appreciated!


More From » html

 Answers
7

You can use the File and FileReader objects to read local files.



You can use an input element with type=file to allow the user to select a file.



<input id=myFile type=file/>
<textarea id=myTextArea rows=4 columns=20></textArea>


After the user has selected a file, you can get the File object from the input element. For example...



var file = document.getElementById(myFile).files[0];


You can then use a FileReader object to read the file into the text area. For example...



var reader = new FileReader();
reader.onload = function (e) {
var textArea = document.getElementById(myTextArea);
textArea.value = e.target.result;
};
reader.readAsText(file);

[#64442] Sunday, November 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
valentinam

Total Points: 166
Total Questions: 117
Total Answers: 81

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;