Editing Files on the web server with Advanced HTML Editor 2
Can this control be used to edit an HTML file on the web server (permissions permitting of course) rather than sending its contents to a record on a database? Answer:
Initially Advanced HTML Editor 2 does not provide auto-generated code for working with files on the web server.
But, this functionality can be achieved by adding the following code at the beginning of your page for loading and saving the files:
* Initialize the file to be loaded
<%
'Map the logical path to the physical system path
Dim Filename
Filename = "index.htm"
Dim Filepath
Filepath = Server.MapPath(Filename)
%>
* Code for witting the file to the server:
<%
'Check if the post is from AHE form and AHE have text
If (CStr(Request("MM_update")) = "form1") And (Request.Form("editor1") <> "") Then
set tFSO = Server.CreateObject("Scripting.FileSystemObject")
'Create / Overwrite the file
set myFile = tFSO.CreateTextFile(Filepath, true)
myFile.WriteLine(Request.Form("editor1"))
myFile.Close
set tFSO = nothing
set myFile = nothing
End if
%>
Note:
The form should include hidden filed named MM_update with value set to form1
* Code for reading the file from the server:
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
set FSO = Server.CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(Filepath) Then
' Get a handle to the file
Dim hFile
set hFile = FSO.GetFile(Filepath)
' Open the file
Dim TextStream
Set TextStream = hFile.OpenAsTextStream(ForReading, TristateUseDefault)
' Read the file line by line
Dim strHtml
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline
' Store the line into strHtml
strHtml = strHtml & Line & vbCRLF
Loop
Set TextStream = nothing
Else
strHtml = "<h3><i><font color="red>File" does not exist</font></i></h3>"
End IF
%>
It saves the content of the file into the variable strHtml
Note: If the file does not exist it set strHtml to "<h3><i><font color="red>File" does not exist</font></i></h3>"
You have to write strHtml variable into the textarea element of the Advanced HTML Editor 2. Also do not forget to include hidden field with name MM_update and value set to form1:
<form id="form1" name="form1" method="post" action="">
<textarea dmxedit="true" id="editor1" name="editor1" style="width:100%;height:400px;"><%= strHtml %></textarea>
<input type="hidden" name="MM_update" value="form1">
<label>
<input type="submit" name="button" id="button" value="Save" />
</label>
</form>
DISCLAIMER:
This is extra complimentary content which purpose is to show additional
usage that is not part of the product, i.e. it suggests tips for
extending the functionality of the product by the users themselves.
It is provided "as is", without warranty of any kind, express or
implied , including but not limited to the warranties of
merchantability, fitness for a particular purpose and nonfringement of
third party rights.
DMXzone does not take responsibility to
support the suggested content to be fully compatible and working as
intended and does not provide support for extended functionality which
is not included in the current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Save/Edit as text File
I am going to purchase Advanced HTML Editor 2 to implement an Admin Area where my client can change the content of their site. However, their site is in Flash and pulls the text from a text file.
The text file stores it as content="<p>Content goes here in HTML code format</p>"
Is there some way I can get Advanced HTML Editor 2 to pull in this code so they can edit it, and then for it to save it back to the text file with content=" in front and a " after it?
You must me logged in to write a comment.