How to read and display html file

Hello man

I'm making a html editor but the problem is that when someone needs to view an html file i don't know how to display the text from the file.

<input type="file" onchange="previewFile()"><br>
<p id="txt1" src="blank.html"></p>
<script>
function previewFile() {
  const preview = document.getElementById('txt1');
  const file = document.querySelector('input[type=file]').files[0];
  const reader = new FileReader();

  reader.addEventListener("load", function () {
    // reads image file to a blob string
    preview.src = window.URL.createObjectURL(file);
  }, false);

  if (file) {
    reader.readAsDataURL(file);
  }
}
</script>

I used this script to read a file and display it by changing the paragraph src to a blob link, but looks like the paragraph dosn't get the text by a html text link using src. Is there a way to do it?

n1md7

The paragraph does not have src attribute, You need to use either innerText or innerHTML to display the content.

function previewFile() {
  const content = document.querySelector('.content');
  const [file] = document.querySelector('input[type=file]').files;
  const reader = new FileReader();

  reader.addEventListener("load", () => {
    // this will then display a text file
    content.innerText = reader.result;
  }, false);

  if (file) {
    reader.readAsText(file);
  }
}
<input type="file" onchange="previewFile()"><br>
<p class="content"></p>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to read a text file and display it in html (angularjs)

How to read a CSV file with hindi character and display on html?

how to read json and display in html

How to use Node.js to read data from a json file and display the read data into html?

how to read html file on Django

how to read specific row from excel file to display information on html output using PHP code?

How to display html table looping through multiple rows read from a file

How to read a local json file and display

How to use Qt to read a file and display it quickly?

How to read PDF file and display it on JTextPane?

React Native: How to read and display xml file

read csv file from a location and display as html table

Read and display an element in a html(xml) file by using Xpath in oracle form

How to display Text file on clicking button in html

How to display LaTex code in HTML file

How to display HTML file in Qt application in realtime

How can I display a tooltip on an HTML file?

How to display simple html file in Qt

how to display another html file in div

How to display html file in iframe using javascript

How to display JSON file on HTML page

How to display a local html file in Android?

How to display txt file contents in a HTML page?

How to read the external html file as a DOM tree?

How to read HTML Text from JSON file

How to save and read output of read_html as an RDS file?

How to read Google sheet cells and display it to an HTML form

how to read xml parse in jquery and display in html element

How to display characters read from a file using getc()