Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display file's contents in a HTML text Area 1

Status
Not open for further replies.

iolaper

MIS
Jun 7, 2004
98
US
Hi everyone,
I used this command to browse..
<input type=file name=uploadfile size="33">

I now want to display the contents of this file in a text Area in my html form. Can any of u give me the method to read and display the file's contents into a text Area?

Thanks so much in advance.
 
OK, assuming that you're talking about text files (.txt, .xml, .html etc) and you want to do this without actually uploading the file to your server, this is how I would go about it:

First, use the file upload control to let the user select the file they want to display. Then use a hidden IFRAME to load the file into the browser. From there you should be able to get at the text in the IFRAME.

Code:
<html>
<head>
<script>
// This function is called when the user selects a file
function doDisplay(objUpload){
 //assign the file to be the source for our temp IFRAME
 document.getElementById("temp").src = objUpload.value;
 //wait until the IFRAME finishes loading the file
 checkReady();
}
// This function polls the IFRAME to see if it has finished loading
function checkReady(){
  if(document.getElementById("temp").readyState == "complete"){
   setTimeout("doTransfer()", 250);
  }
  else{
   setTimeout("checkReady()", 250);
  }
}
//This function copies the text in the IFRAME window to the TEXTAREA
function doTransfer(){
  document.getElementById("final").value = document.frames[0].document.documentElement.innerText;
}
</script>
</head>
<body>
<!-- Our input for getting the file -->
<input type=file name=uploadfile size="33" onchange="doDisplay(this)">
<!-- our "holding pen" iframe -->
<iframe id="temp" style="display:none"></iframe><br>
<!-- our text area in which to display the text of the file -->
<textarea id="final" rows="25" cols="100"></textarea>
</body>
</html>

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top