Is it possible to display the same information on a textfile on a web application so my programme can read directly from a textfile ?

so if any changes happen on my textfile it must also take effect in my programme without me doing anything

Dani AI

Generated

As asked and as hinted, the approach depends on where the text file lives. If the file is on a server you control you can make clients reflect changes automatically; if it lives on a user’s local disk, browsers will not read it without explicit user consent or a helper app.

For a server-hosted file the common, reliable pattern is: have a server-side watcher detect file changes, then push updates to connected clients. Pushing is more efficient than frequent polling for real-time updates. A minimal flow: watch the file (debounce the events), read the file after change, then broadcast the new contents via WebSocket or Server‑Sent Events to all clients. Example server/client pair (Node + WebSocket):

const chokidar = require('chokidar');
const WebSocket = require('ws');
const fs = require('fs');

const wss = new WebSocket.Server({ port: 3000 });
chokidar.watch('data.txt').on('change', () => {
  const text = fs.readFileSync('data.txt', 'utf8');
  wss.clients.forEach(c => c.readyState === WebSocket.OPEN && c.send(text));
});

Client:

const ws = new WebSocket('ws://localhost:3000');
ws.onmessage = e => document.getElementById('output').textContent = e.data;

If the file is on a local machine, browsers intentionally block automatic reads. Options are: ask the user to pick the file with the File System Access API (requires user permission and HTTPS — see MDN), run a small local service/agent that serves the file on localhost, or package the app with Electron/a native app. See Server‑Sent Events and the File System Access API for browser-side capabilities: Server‑Sent Events and File System Access API.

Practical tips: debounce watcher events; wait briefly before reading to avoid partial writes; set proper CORS and caching headers; sanitize content before injecting into the page; use authentication if the file is sensitive. This gives a robust, low-latency update path whether you choose push (WebSocket/SSE) or a polite polling fallback.

Recommended Answers

All 2 Replies

Yes, this is possible. There are four options to be able achieved this.

  1. Use PHP
  2. Use the HTML5 file reader
  3. Use javascript
  4. ASP.net

Try google with 'reading text file in javascript' and for PHP read this. If you are developing in asp.net mvc there is a method called ReadAllText, I don't remember the proper syntax, but it should be on the Microsoft website.

Member Avatar for Member #120589

Do you need to read a client file or a server file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.