I am trying to achieve this: I have a static html page, which needs to load text from a text file, as part of its content. The text file is in the same root folder of the website, which is also where the html page reside.
I am aware that simple html cannot load text from a file; so I am quite clueless about how can I achieve this. The server cannot use dynamic pages (it is quite complex to explain; I am using a web server that I cannot configure; I can just write and run static HTML pages on it).
Is there a trick to read a text file and slap the content on a webpage? Otherwise I am forced to have to create this webpage from scratch every time that the text file changes; using python somehow (I am no expert); which seems like a lot of work.
I am not sure what you want to do,reading a single txt file everytime or you might want to change the txt file.What you can o is you can use javascript file reader to read the file. And then you can post the content of the file into the html where ever you want.For a look up,
your javascript file would look like this,rename it as test.js
window.onload = function() {
var fileInput = document.getElementById('fileInput');
var fileDisplayArea = document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function(e) {
fileDisplayArea.innerText = reader.result;
}
reader.readAsText(file);
} else {
fileDisplayArea.innerText = "File not supported!";
}
});
}
your html page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File API</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="page-wrapper">
<h1>Text File Reader</h1>
<div>
Select a text file:
<input type="file" id="fileInput">
</div>
<pre id="fileDisplayArea"><pre>
</div>
<script src="text.js"></script>
</body>
</html>
you can mould it to your requirement,like passing the url of your file.I hope this must be what you searching for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With