Log in

View Full Version : Save <textarea> results into *.txt file



tech_support
06-20-2006, 06:45 AM
Hi!

Can you save the results of a <textarea> to a *.txt file? Or maybe a *.htm or *.html file by pushing a button?

I saw it once in the "Windows Vista Upgrade Advisor" so I think It's possible

But is it? :confused:

I would really appreciate it if I can get the script for it :D

Vallim
06-20-2006, 01:35 PM
Hi,

You could use the below code to write the contents of the <textarea> to a textfile. Here the ActiveXObject to write to the file.As per the coding NewFile.txt will be created under the C drive, and all <textarea> content will be placed in it.

<html>
<head>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerText;
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
</script>

</head>
<body>
<form>
<div>
<textarea id="TextArea1"
height: style="width: 588px; height: 90px" 90px">We are a leading provider of software components and tools for the Microsoft .NET platform. Powerful and feature-rich, Syncfusion’s broad range of products are vital to mission-critical applications in organizations ranging from small businesses to multinational Fortune 100 companies. -www.syncfusion.com
</textarea><br />
<input id="Button1" type="button" value="Write" onclick="WriteToFile()"/>
</div>
</form>
</body>
</html>

Let me know if you have any doubts.

Regards,
Valli

[www.syncfusion.com
http://www.syncfusion.com/faq/aspnet/default.aspx]

Twey
06-20-2006, 02:13 PM
If you're stupid enough to use ActiveX in your websites, thereby royally cheesing off at least 30% of your visitors and causing a massive popup box to appear warning the rest that your site is trying to do something unsafe, go right ahead! :)

If, however, you're a sane and intelligent being, you have two options here: cookies or server-side storage. The latter is preferable, since it won't expire or be deleted, but you may not have server-side scripting support on your server.


NewFile.txt will be created under the C driveI don't have a "C drive." In fact, even Windows users might not have a "C drive."

shachi
06-20-2006, 03:42 PM
Hey try this: http://calle.ioslo.net/testing/textarea-save/

Uses cookies to store contents. And is better than using any Server side complicated code.:)

Twey
06-20-2006, 04:31 PM
And is better than using any Server side complicated code.Server-side is always a better idea, since it ensures data integrity.

shachi
06-20-2006, 04:50 PM
Twey for me (who is very bad at server side languages) it's really complicated.(you've seen it isn't it?? In the PM?? I couldn't even solve that simple problem.:( ). But anyways I am learning PHP and that's better than not knowing any server side language.:)

Twey
06-20-2006, 05:04 PM
Twey for me (who is very bad at server side languages) it's really complicated.That's fine, but it's not a good idea to recommend that other people avoid them just because you don't understand them. :)

shachi
06-20-2006, 05:30 PM
By the way Twey did you realize that this reply textarea can actually perform events when Ctrl+B is pressed?? It actually does override the default firefox bookmark event. Any ideas how it is done?? I'd be appreciated if you could find it out. And by the way I never said that avoid it what I meant was that this code is simpler than that of server side languages and is easily usable. I am sorry If I said anything wrong.:)

djr33
06-20-2006, 05:35 PM
I agree with Twey here.

I'm not sure if you want the file stored on the server or on the user's harddrive, but either way, you should use server side. It really isn't that complex. This is a simple code.

You have two options:
1. Code it to save to a text file on the server. Done.
2. Code it to output the text file to the user, so they can save it as they want. This won't force anything onto their system (usually a bad idea), and will be fairly easy still.

Twey
06-20-2006, 05:55 PM
And by the way I never said that avoid it what I meant was that this code is simpler than that of server side languages and is easily usable.It was "better" I had an objection to. :)

tech_support
06-21-2006, 03:14 AM
OK,

I don't want to use server-side (cause it's an offline thing)

Cookies.... umm... not really want to

So I think I'll stick to ActiveX (Thanks Vallim!) :)

And one question here:

Can you make it so that you can enter a path and save the file where ever you like?

Here we have it so it saves in C:\\NewFile.txt:
And I want it to save wherever the user likes it.



<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerText;
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
</script>

Twey
06-21-2006, 05:59 AM
Can you make it so that you can enter a path and save the file where ever you like?It's just a string. Use window.prompt() or a more advanced method, as you like.
Cookies.... umm... not really want to

So I think I'll stick to ActiveX (Thanks Vallim!):eek:

tech_support
06-21-2006, 06:32 AM
I mean something like this:

http://img67.imageshack.us/img67/1121/save0nh.th.png (http://img67.imageshack.us/my.php?image=save0nh.png)

(If you can't see it click this link: http://img67.imageshack.us/my.php?image=save0nh.png)

Not prompt I'll leave that till the last option :)

djr33
06-23-2006, 09:35 AM
er...

var s = fso.CreateTextFile("PATH\\file.txt", true);

So.... C:\\, or C:\\documents%20and%20settings\user\desktop\...

?

tech_support
06-23-2006, 09:58 AM
Does that include the "Save" dialog?

djr33
06-23-2006, 09:10 PM
No, the code specifies a place on the harddrive; it would be a different code if you needed the browse/save window.
And, no, sorry, no clue how you'd get that.

Except... you could do what I suggested earlier... run it serverside, get the php to output a text file, then you could save that to your harddrive, like any download... there ya go.

tech_support
06-24-2006, 03:24 AM
run it serverside, get the php to output a text file, then you could save that to your harddrive, like any download...

How do you do that then?

djr33
06-24-2006, 07:42 AM
FORM:
<form action="save.php" method="post">
<textarea name="text"></textarea>
<input type="submit" value="send">
</form>

PHP PAGE:
<?php
header ("Content-Type: application/txt");
readfile($_POST['text']);
?>

This is untested, but I think it should work. Certainly echo $_POST['text']; would work, just not sure about outputting it to be saved.

I'm not too familiar with outputting a file... more info here:
http://php.net/manual/en/function.readfile.php

Twey
06-24-2006, 07:55 AM
Er, no. :)

$fn = "tmp" . md5(microtime()) . ".txt";
$file = fopen($fn, "w");
fwrite($file, $_POST['text']);
fclose($file);
header("Location: " . 'http://' . $_SERVER['HTTP_HOST'] . substr($_SERVE
R['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/") + 1) . $fn);

djr33
06-24-2006, 07:55 AM
There ya go :)


:p

Twey
06-24-2006, 08:10 AM
That will kind of leave unwanted text files with names like "tmp0123456789abcdef.txt" all over the server, though. :p

djr33
06-24-2006, 08:26 PM
Good news... it would leave a log of what users did.

Or... why use a random name for each file... you could just have temp.txt be the same thing for each person... they could all download the same file, and have it constantly updated.
(though there are security holes here; and what if two people were doing it at the exact same time?)

tech_support
06-25-2006, 05:15 AM
One problem here.

Would that save the file to the server or the user's hd?

djr33
06-25-2006, 07:00 AM
It would save a (temporary) file to the server that would be given to the user to download, giving them the choice of downloading or not and where they'd like to save it, as well as what name to use.

tech_support
06-26-2006, 08:06 AM
Oh... ok!

Thanks

vidkid
04-02-2008, 11:19 AM
Hey, I'm a newbie trying to use the script given to run the Active X solution above. I simply want to be able to save some form field entries locally on a PDA as a text file (hopefully comma delimited) so it can be sent later to a database.

The above sounds just right, but I think i need more help with completing the form, as when i use it nothing happens. Apologies, like i said im a very new newbie so hand holding would help.

Tanks in advance,