Reputation: 149
I want to upload a text file on a webpage, read it, and then in javascript do some computation on the content of the file, e.g by calling a function called:
do_calculation().
Here is the code I have so far. But it is not complete as I don't know how to read the content of the file from: function do_calculation().
html code:
<tr>
<td>Select a File to Load:</td>
<td><input type="file" id="pubkey"></td>
<output id="result" />
<td><button onclick="loadFileAsText()">Load Selected File</button><td>
</tr>
Javascript code:
<script type="text/javascript">
function loadFileAsText(){
var fileToLoad = document.getElementById("pubkey").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent){
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("result").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
function do_calculation(){
}
</script>
Moreover, I'd like to know how I can work on two uploaded files.
Upvotes: 0
Views: 84
Reputation: 1229
Just call your do_calculation()
function in your filereader.onload
fucntion and give it the parameter of the text. Then you can do whatever you want with the text at that point within your do_calulation()
function
Jsfiddle: https://jsfiddle.net/yxmngk0r/
Hopefully this helps.
Upvotes: 1