...

View Full Version : load/save textarea



jalowery
09-01-2010, 08:35 PM
I am trying to do this and have some code im working with.

I can load the file through a browse button then save it using the existing code.

However, I want instead of the browse option to just preload the file into the textarea and then be able to save it.


<script language="javascript">
<!--//
var oFileSystem;
oFileSystem = new ActiveXObject("Scripting.FileSystemObject");
function ShowFile(sFilePath){
frmEditHTML.tarHTMLText.value = oFileSystem.OpenTextFile(sFilePath).ReadAll();
}

function SaveAfterEditing(){
var oFile;
oFile = oFileSystem.CreateTextFile(frmEditHTML.filPath.value,1);
oFile.WriteLine(frmEditHTML.tarHTMLText.value);
oFile.Close();
}
</script>



<form name="frmEditHTML">
<input type="file" name="filPath" onchange="ShowFile(this.value)">
<br><textarea name="tarHTMLText" cols=60 rows=20></textarea><br>
<input type="button" value="Save" name="cmdSave" onclick="SaveAfterEditing()">
</form>

Old Pedant
09-01-2010, 09:53 PM
You do know this will only ever work in MSIE and even then only if the user has marked your site as "Trusted" and even then only if the user accepts your unsafe code each time the page is invoked?

Given the above, that code *MIGHT* work. It depends on whether MSIE will put the full path to the file into the form field's ".value" propery or whether it puts in only the file name, not the path.

Okay...I just checked: MSIE 8, at least, seems to put the full path there. So it should work.

In fact, I just plunked your entire code down into an HTML file and tried it. It worked like a charm.

So what is the question???

Old Pedant
09-01-2010, 09:56 PM
Oh, stupid me.

I should read your QUESTION!!

Well, it's easy:


<html>
<head>
<script language="javascript">
var oFileSystem = new ActiveXObject("Scripting.FileSystemObject");
var filePath = null;

function ShowFile(sFilePath){
document.frmEditHTML.tarHTMLText.value = oFileSystem.OpenTextFile(sFilePath).ReadAll();
filePath = sFilePath; // save for later
}

function SaveAfterEditing(){
var oFile;
oFile = oFileSystem.CreateTextFile(filePath,1);
oFile.WriteLine(frmEditHTML.tarHTMLText.value);
oFile.Close();
}
</script>
</head>
<body onload="ShowFile('c:\\test\\junk1.html');">
<form name="frmEditHTML">
<br><textarea name="tarHTMLText" cols=60 rows=20></textarea><br>
<input type="button" value="Save" name="cmdSave" onclick="SaveAfterEditing()">
</form>
</body>
</html>


Use your own full path, of course.

jalowery
09-02-2010, 09:41 PM
Thank you very much.

That solution worked perfectly. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum