Read contents from input type file

Read input type file contents –
File content can be read using FileReader function of javascript. Here is an example for explanation –

Create HTML (input field and a div where we have to show the content of file) –

<input type=”file” name=”inputfile” onchange=”readinputfile()”>
<div class=”showfilecontent”></div>

Now call JS function –

const reader = new FileReader()
function readinputfile (input) {
const input = input.files[0]
reader.readAsText(input)
}
reader.onload = function (e) {
document.querySelector(‘. showfilecontent ‘).innerText = e.target.result;
}

Leave a Reply

Your email address will not be published. Required fields are marked *