:: Forum >>

Load file into javascript variable

I like your site and the activewidgets grid. I am not using it yet. I am just beginning to educate myself about XMLHTTP and the Sarissa Library.

I want to share this piece of code I wrote that you may be able to use and promote to your visitors. I wrote it because I found various place on the net that said it is not possible to read a file and load the content into a variable. I got around the issue by reading the content of a particular div into a variable in the file that is being read.

Save the following code into a file:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
function loadFile() {
var divHandle = document.getElementById("mydiv");
var saveText = divHandle.innerHTML;
divHandle.innerHTML = 'Transform the contents: <font color:green>'+saveText+'</font>';
alert("contents:"+saveText);
}
</script>
</head>

<body onLoad="loadFile()">
<div id="mydiv">
Line 1
Line 2
Line 3
</div>
</body>
</html>

Now in your browser open the file.

Once you have the contents in a variable you can use to replace the contents of the original or even call the variable from another frame.

My website is fixmywebsite.net
Fidel Guajardo
Wednesday, July 20, 2005
You are not 'reading the file' you are loading a web page. What you can not do is from a web page, use the <input type="file"> tag and truly load a file and then manipulate the contents of that file after it is loaded. At that point all you have is the name of the file (after the user clicks Browse and selects a file), you do not have access to the contents of the file they selected. Once the form is submitted, a copy of the file gets sucked up off the hard drive and set to the server. I have tried everything under the sun to be able to load a file from disk then do something with that file but the JavaScript Gods tell us that the information is off limits!
Jim Hunter
Thursday, August 18, 2005
Hi,
I want to know how load a text file into a browser? step by step please.
Thanks
Fawzia
Glasgow
UK
Fawzia Alshubaily
Sunday, June 25, 2006
Well, like Jim said before, u cant just "load the file" to browser, u need upload the file first.
Use iframe to send file to server:
<html>
<body>
function showFileText(txt) {
alert(txt);
}
<iframe id='sendFile' name='sendFile' src='sendFile.html'>
</iframe>
</body>
</html>

// sendFile.html
<html>
<body>
<form id='sendFileForm' name='sendFileForm' method='post' action="sendFileServlet.do" enctype="multipart/form-data">
<input type='file' id='fileUpload' name='fileUpload'>
<input type='submit' id='sub' name='sub' value='Upload File'>
</form>
</body>
</html>

// servlet response
<html>
<body>
<script>
var txt = '.........................................'; // file content
top.window.showFileText(txt);
</script>
</body>
</html>

Servlet read the file and set variable "txt". A javascript function access the Parent window (main window) and show the content.
Paulo Cesar Silva Reis (PC from Brazil).
Sunday, June 25, 2006

This topic is archived.


Back to support forum

Forum search