views:

92

answers:

3

i have a textarea and two buttons

like

<form name="form1">
<textarea name="text1"> HTML Codes goes here </textarea>
<input type="button"> Open File
<input type="button"> Save File
</form>

when i click on "save" button i want the text in textarea to be saved (i want it to pop up the "save as" dialog box)

When i click on "open" , it should allow me to choose any html or textfile... and load the text in the textfile/htmlcode into my textarea.

Found this code in http://www.dynamicdrive.com/forums/archive/index.php/t-10532.html

  <html>
<head>
</head>
<body>
<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>

<form name="abc">
<textarea name="text">FIFA</textarea>
<button onclick="WriteToFile()">Click to save</Button>  
</form> 

</body>
</html>

this would work if it porvides the user the choice to save the file ...and i forgot to say that all files are in the client computer.

Thanx in Advance

-Miss Subanki

+2  A: 

Saving - You have to do that server-side, but it isn't difficult; in PHP you would just force some HTTP headers before outputting the data:

// set the content type
header('Content-type: text/plain');
// force save as dialog (and suggest filename)
header('Content-Disposition: attachment; filename="download.txt"');
// next echo the text
echo $_POST['text'];

Opening - You have to handle the uploaded data server-side, unless you use some proprietary (albeit "open") API like in firefox.

Christian Sciberras
i just want it to fetch the html codes in a html file when we click on the open button...are u sure it cant be done
subanki
But from where? If it's the server then it's ok, if it's the client, then it can't be done transparently.
Christian Sciberras
What if you used your code to read the user's history or saved passwords list? With the "open button/dialog" the user is forced to choose the fail, thus making it somewhat safe (unless the user is seriously dumb ;-) ).
Christian Sciberras
all files are in the client computer what is the use of using a server side scripting
subanki
Because the file needs to be sent to the server, as I said, unless you use very modern code (which afaik is still experimental to firefox) you can't handle "file uploads" client side.
Christian Sciberras
+1  A: 

You can save a file with Javascript, but you have to use execcommand and then you'd be limited to Internet Explorer.

JC Leyba
that is fine can u provide me the code so that i can test it ..i am little noob in coding
subanki
Try this.Not sure if that's what it is. Haven't used javascript in so long. A quick Google search on execCommand will reveal more. document.execCommand('SaveAs', true);
JC Leyba
thanx to JC Leyba
subanki
in javascript it is possible to save the text in a textarea....using document.execCommand('SaveAs', true);
subanki
+1  A: 

This is possible only in IE, since the ActiveXObject has access to files (Read/Write). so, you can do it

I still remember, I tried it once and got succeeded

But Remember it only works in IE

Ninja Dude