Pour charger un fichier txt dans une page html :
Le JsFiddle est ici (juste pour montrer l’aspect de la page HTML car il est nécessaire d’avoir un fichier txt !)
<html>
<head>
<title>textbox</title>
<script type="text/javascript">
function readBOX() {
var txtinput = document.getElementById('txtinput').value;
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', txtinput, false); //-- xmlhttp.open('GET', 'log.txt', false); //--fichier txt sur le serveur !
xmlhttp.send();
text = xmlhttp.responseText;
document.forms[0].text.value = text;
}
</script>
</head>
<body>
Fichier (log.txt)
<input type="text" id="txtinput" />
<input id="open" type="button" value="READ" onClick="readBOX()" />
<form>
<textarea name="text" rows="20" cols="70">loaded text here</textarea>
</form>
</body>
</html>
Chargement automatique du fichier dans la page :
<html>
<head>
<title>textbox</title>
</head>
<body>
<table style="text-align: left; width: 309px; height: 34px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top; background-color: rgb(204, 204, 204);">
<div style="text-align: center;">
<a href="statut.htm" ><input value="Retour" type="button"> </a>
<a href="statut.htm" ><input value="Effacer" type="button"> </a>
<a href="envMail" ><input value="Envoyer" type="button"> </a>
<a href="log.txt" > <input value="Télécharger" type="button"> </a>
</div>
</td>
</tr>
</tbody>
</table>
<textarea name="text" id="text" rows="20" cols="40">loaded text here</textarea>
</body>
<script type="text/javascript">
function readTxt() {
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'log.txt', false); //--fichier txt sur le serveur !
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById('text').value=text;
}
window.onload = readTxt();
</script>
</html>
Votre commentaire