View Full Version : Saving a text area to a file.

07-24-2003, 08:31 AM
Hi anyone who reads this

Ive done some searching on the net but have been unsuccesful in possibly finding a way to save the contents of a Text Area or even a Text Field to a file onto my machine or the users machine.

Does anyone know how to do this ? is it possible ?
surely it is huh :) -> :confused:

Philip M
07-24-2003, 08:51 AM
Javascript has no capability to write to a file. You need a server side language such as PHP or PERL to do this.

07-24-2003, 09:08 AM
PHP, Perl, ASP will do it easily, the thing that boggles me is that this topic was moved!?? To javascript!!

07-25-2003, 07:39 PM
I was able to have javascript using active X save
a text file to the clients machine, but it had problems.

I have since started writing a file on the server side
and using a link to the filename so when the user
clicks on it, it downloads like any other file.

Using the Active-X required too much tweaking of
the security settings than I wanted.

I don't have the coding anymore on what I used
to write to the file.



07-26-2003, 03:19 PM
Here a Sample which saves the textarea-content in a textfile. It works with JavaScript and with VBScript, but only eith Internet Explorer.

And, of course, the file is not saved on the computer where you are using the script from - not on the server who hostes this file.

<title>Saving textarea to file - www.united-scripts.com</title>
<script language="vbscript">
sub save_text_vbs()
set fs = Createobject("Scripting.FileSystemObject")
file_location = "c:\textarea.txt"
set file = fs.CreateTextFile(file_location, True)
file.Write document.getElementById("text").value
document.getElementById("result").value = "File successfully saved in " & file_location
end sub
<script language="javascript">
function save_text_js()
var file_location = "c:/textarea.txt";
var fs = new ActiveXObject("Scripting.FileSystemObject");
file = fs.CreateTextFile(file_location, true);
document.getElementById("result").value = "File successfully saved in " + file_location;

<textarea cols="40" rows="10" id="text" style="width:400px;"></textarea><br>
<input type="button" onclick="save_text_vbs()" value="Save using VBScript (only in IE)" style="width:400px;"><br>
<input type="button" onclick="save_text_js()" value="Save using JavaScript" style="width:400px;"><br>
<input type="text" id="result" style="width:400px;border: 0px solid white;">

(This will only work if your scuritiy settings are permitting creating ActiveX Objects by Brwoser Scripting)


07-28-2003, 03:46 PM
yes, those were the links to the vbcode/active x
I was using.

The problem I encountered, was when the file
existed already. Sometimes the file would
get overwritten, other times the existing data
wouldn't get overwritten. It was inconsistant.

That may be OS dependent, or maybe it was
something at the time that was IE/VB/Javascript
problem that may have been corrected in later

Unless this is on an intranet, with all the potential
security issues with activeX and websites, as stated
it's almost not worth the use.

But, it did work, and wasn't hard to implement.