5

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

13
  • Nah, the match isn't starting for over an hour, plenty of time to hang around on SO. I think no-one's answering because you're asking something that's impossible... but I'll leave the definite answer to the JavaScript gurus. Commented Jul 11, 2010 at 17:14
  • Thanks for supporting us by the way! :D Commented Jul 11, 2010 at 17:14
  • oh you from netherlands, no need to thank , they played very good....now i am a big fan of netherlands....they deserve to win ... Commented Jul 11, 2010 at 17:18
  • 3
    First, that code only works in MSIE (use of activex). Second, it probably won't run if not from a local context (eg, html on your pc). Third, it is pretty obvious why that code shouldn't even work, as I've said, it is very insecure to allow that functionality. Commented Jul 11, 2010 at 17:32
  • 1
    If you post to page2 with method="get" on your form, the value of the textarea will be in the URL as ?text=value%20from%20textarea. Using javascript you can get this value like this var val = window.location.search.replace(/?text=([^&]+)/, "$1"); then you can print the value document.write(val). The only problem is to set the headers Content-type and Content-Disposition. I don't know if you can do that without a server side help. That would generate a SaveAs dialog and the file would have the textarea value. Commented Jul 11, 2010 at 17:40

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
But from where? If it's the server then it's ok, if it's the client, then it can't be done transparently.
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 ;-) ).
all files are in the client computer what is the use of using a server side scripting
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.
1

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

document.execCommand('SaveAs', true);

3 Comments

that is fine can u provide me the code so that i can test it ..i am little noob in coding
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);
in javascript it is possible to save the text in a textarea....using document.execCommand('SaveAs', true);
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.