ASP automatic HTML file generation method

Source: Internet
Author: User
Tags newsfile
1. design the database testmb. MDB
Create a table moban: Field m_id (automatic number, primary keyword); field m_html (Remarks type)

2. Assume that the content code of the first template is

Copy the following code to the m_html field.
The following is a code snippet:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> testmb </title>
</Head>
<Body leftmargin = "0" topmargin = "0">
<Table width = "100%" Height = "100%" border = "0" cellpadding = "5" cellspacing = "2">
<Tr align = "right" bgcolor = "# cccccc">
<TD Height = "20" colspan = "2"> $ cntop $ </TD>
</Tr>
<Tr valign = "TOP">
<TD width = "25%" bgcolor = "# e5e5e5"> $ cnleft $ </TD>
<TD width = "74%" bgcolor = "# f3f3f3"> $ cnright $ </TD>
</Tr>
</Table>
</Body>
</Html>
Note $ cntop $, $ cnleft $, and $ cnright $. They will implement certain program functions.

3. Create the database connection file conn. asp
The following is a code snippet:
<%
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("testmb. mdb ")
Conn. Open connstr
%>
4. Create the library file Lib. asp required for special String Conversion

The main function of this file is to make ASP programs that implement certain functions into word programs for convenient calls.
The following is a code snippet:
<%
Dim topcode
Sub cntop ()
Topcode = "the current time is :"
Topcode = topcode & now ()
End sub

Dim leftcode, I
Sub cnleft ()
For I = 1 to 5
Leftcode = leftcode & "<p> cnbruce.com"
Next
End sub

Dim rightcode
Sub cnright ()
For I = 1 to 9
Rightcode = rightcode & "<HR color =" & I & ">"
Next
End sub
%>
5. Finally, call the template code in the database to convert special strings.
The following is a code snippet:
<! -- # I nclude file = "conn. asp" -->
<! -- # I nclude file = "Lib. asp" -->
<%
SQL = "select * From moban where m_id = 1"
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 1, 1
Mb_code = RS ("m_html ")
Rs. Close
Set rs = nothing

Cntop ()
Mb_code = Replace (mb_code, "$ cntop $", topcode)
Cnleft ()
Mb_code = Replace (mb_code, "$ cnleft $", leftcode)
Cnright ()
Mb_code = Replace (mb_code, "$ cnright $", rightcode)

Response. Write mb_code
%>
This page is mainly used to display the template code and convert the special code into the corresponding subroutine function.

At this point, the ASP template function is basically complete, and the rest is: create a program page with the template editing function, change the library file to your desired program function ......

2. How can we implement 2html technology? How can I convert an ASP page to HTML? The FSO component can be used to create any file format.

So what is the overall running process?
A. Provide the Information Input page for information collection;
B. Save the database before receiving the information value, and then generate a file using FSO;
C. Technically complete the task and display the path of the newly created HTML file.

The implementation process of this technology has the following difficulties:

I. Is the file generated by FSO directly placed in a large folder or separately in a daily update subfolders? The statement may be inaccurate. I believe that over time, the number of files generated through FSO will increase, and management will become increasingly messy ...... Generally, you may see some URLs such as www.xxx.com/a/2004-5-20/200405201111.html. It can be analyzed that the folder with the current date is created. In this way, one day is the page content of a folder, so it is reasonable to view and manage it.

Ii. When I tried to create a folder using the above method, I found the second problem. It is no problem to create a folder named after the current date through FSO for the first time. When I have a new file to be generated, because it is the same program, it will create the same folder again. In this case, the FSO component will find that the path already exists ...... Case -_-! To continue the process, add the code in the first line:

On Error resume next

Hey hey, you have achieved the effect of deceiving yourself and stealing your ears.

Iii. How do I create a folder? This mainly refers to the generation of file names. Of course, you need to write a function by yourself. The function is to generate a file name :)

<%
Function makefilename (fname)
Fname = fname'' fname is a variable, and fname is a function parameter reference.
Fname = Replace (fname ,"-","")
Fname = Replace (fname ,"","")
Fname = Replace (fname ,":","")
Fname = Replace (fname, "PM ","")
Fname = Replace (fname, "am ","")
Fname = Replace (fname, "Morning ","")
Fname = Replace (fname, "Afternoon ","")
Makefilename = fname & ". html"
End Function
%>

When referencing a function:
<% Fname = makefilename (now () %>

In fact, it is a file named after year, month, day, hour, minute, and second.

Iv. How can I view the generated file? Of course, you need to save the path of the generated file to the database and add it to the corresponding record set. Of course, this will be mentioned in the following database design.

3. Combination of template technology and 2html technology: replace the value of special code in the template with the value accepted from the form to complete the template function; generate HTML files for all the final replaced template codes. Note that the replacement should completely change the format of the input data or the Code supporting UBB.

2. design the database

Currently, two tables are required for Database Design: one for storing template data and the other for storing information.

1. Create a new database asp2html. MDB

2. design the new database table c_moban
Field m_id (automatic number, primary keyword); field m_html (Remarks type ).
Copy the following complete code to the m_html field.

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = Hz">
<Title> cnbruce. com | asp2html test </title>
</Head>
<Body leftmargin = "0" topmargin = "0">
<Table width = "100%" Height = "100%" border = "0" cellpadding = "5" cellspacing = "2">
<Tr align = "right" bgcolor = "# cccccc">
<TD Height = "20" colspan = "2"> $ cntop $ </TD>
</Tr>
<Tr valign = "TOP">
<TD width = "25%" bgcolor = "# e5e5e5"> $ cnleft $ </TD>
<TD width = "74%" bgcolor = "# f3f3f3"> $ cnright $ </TD>
</Tr>
</Table>
</Body>
</Html>

3. Design the new database table c_news

Field c_id: automatic number, primary keyword
Field c_title: text type. Save the article title.
Field c_content: remarks type. Save the article content.
Field c_filepath: text type, retain the path address of the generated file
Field c_time: date/time type; default value: Now ()

3. Page requirement Design

1. Create a folder to store HTML pages

Create a folder newsfile under the same directory of the file. The folder mainly stores the generated HTML page. Of course, the folder will be created by using a program to facilitate browsing and management.

2. Function page Lib. asp

<%
''Function for generating file names
Function makefilename (fname)
Fname = fname
Fname = Replace (fname ,"-","")
Fname = Replace (fname ,"","")
Fname = Replace (fname ,":","")
Fname = Replace (fname, "PM ","")
Fname = Replace (fname, "am ","")
Fname = Replace (fname, "Morning ","")
Fname = Replace (fname, "Afternoon ","")
Makefilename = fname & ". shtml"
End Function

''Functions that keep the data format unchanged
Function htmlencode (fstring)
Fstring = Replace (fstring, ">", "> ")
Fstring = Replace (fstring, "<", "<")
Fstring = Replace (fstring, CHR (32 ),"")
Fstring = Replace (fstring, CHR (13 ),"")
Fstring = Replace (fstring, CHR (10) & CHR (10), "<br> ")
Fstring = Replace (fstring, CHR (10), "<br> ")
Htmlencode = fstring
End Function
%>

3. Database Connection page conn. asp
Complete the database string connection method

<%
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("asp2html. mdb ")
Conn. Open connstr
%>

4. add.html
Actually, it's very simple: form. Note that action is redirected to addit. asp
<Form action = "addit. asp" method = "Post">
Title: <input type = "text" name = "c_title"> <br>
Content: <br>
<Textarea name = "c_content" rows = "8" Cols = "30"> </textarea> <br>
<Input type = "Submit" value = "add">
<Input type = "reset" value = "reset">
</Form>
5. addit. asp
First, process the accepted data and write the value to the database. Then, reference the template code, convert the special code to the accepted value, and generate an HTML page through FSO. Note that the path address of the generated file is saved to the database table.

<% ''Fault tolerance Processing
On Error resume next
%>

<! -- # I nclude file = "conn. asp" -->
<! -- # I nclude file = "Lib. asp" -->

<% ''Accept pass value
C_title = request. Form ("c_title ")
C_content = request. Form ("c_content ")
%>

<% '': Generate the HTML file name, create a folder, and specify the file path.
Fname = makefilename (now () ''makefilename is a custom function.
Folder = "newsfile/" & date ()&"/"
Filepath = folder & fname
%>

<% '': Keep the accept value and path to the database table.
SQL = "select * From c_news"
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 3, 2
Rs. addnew
RS ("c_title") = c_title
RS ("c_content") = c_content
RS ("c_filepath") = filepath
Rs. Update
Rs. Close
Set rs = nothing
%>

<% ''Open the template code and convert the special code to the accept value.
Sql1 = "select m_id, m_html from c_moban where m_id = 1"
Set RS1 = server. Createobject ("ADODB. recordset ")
Rs1.open sql1, Conn, 1, 1
Mb_code = RS1 ("m_html ")
Rs1.close
Set RS1 = nothing
Conn. Close
Set conn = nothing
C_title = htmlencode (c_title)
C_content = htmlencode (c_content)
Mb_code = Replace (mb_code, "$ cntop $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To generate an HTML page
Set FSO = server. Createobject ("scripting. FileSystemObject ")
FSO. createfolder (server. mappath (folder ))
Set fout = FSO. createtextfile (server. mappath (filepath ))
Fout. writeline mb_code
Fout. Close
%>

The article is successfully added. <a href = "/showit. asp"> browse </a>

6. Display database table records and link to the HTML page: showit. asp

<! -- # I nclude file = "conn. asp" -->
<! -- # I nclude file = "Lib. asp" -->

<% Id = request. querystring ("c_id") %>

<%
If request. Form ("Submit") = "change" then
C_title = request. Form ("c_title ")
C_content = request. Form ("c_content ")
C_id = request. Form ("c_id ")
C_filepath = request. Form ("c_filepath ")

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From c_news where c_id =" & c_id
Rs. Open SQL, Conn, 3, 2
RS ("c_title") = c_title
RS ("c_content") = c_content
RS ("c_time") = now ()
Rs. Update
Rs. Close
Set rs = nothing
%>

<% ''Open the template code and convert the special code to the accept value.
Sql1 = "select m_id, m_html from c_moban where m_id = 1"
Set RS1 = server. Createobject ("ADODB. recordset ")
Rs1.open sql1, Conn, 1, 1
Mb_code = RS1 ("m_html ")
Rs1.close
Set RS1 = nothing
Conn. Close
Set conn = nothing
C_title = htmlencode (c_title)
C_content = htmlencode (c_content)
Mb_code = Replace (mb_code, "$ cntop $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To generate an HTML page
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set fout = FSO. createtextfile (server. mappath (c_filepath ))
Fout. writeline mb_code
Fout. Close
%>
<% Response. Redirect ("showit. asp") %>
<% End if %>

<%
If id <> "" then
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From c_news where c_id =" & ID
Rs. Open SQL, Conn, 1, 1
C_id = RS ("c_id ")
C_filepath = RS ("c_filepath ")
C_title = RS ("c_title ")
C_content = RS ("c_content ")
End if
%>

<Form action = "change. asp" method = "Post">
Title: <input type = "text" name = "c_title" value = <% = c_title %> <br>
Content: <br>
<Textarea name = "c_content" rows = "8" Cols = "30"> <% = c_content %> </textarea> <br>
<Input type = "Submit" value = "change" name = "Submit">
<Input type = "reset" value = "reset">
<Input name = "c_id" type = "hidden" value = "<% = ID %>">
<Input name = "c_filepath" type = "hidden" value = "<% = c_filepath %>">
</Form>

7. Modify the data content page Chang. asp

Modify the data content and update the corresponding HTML page. The modification is actually to regenerate the file, and the file name is the same as before, similar to the file overwrite.

<! -- # I nclude file = "conn. asp" -->
<! -- # I nclude file = "Lib. asp" -->

<% Id = request. querystring ("c_id") %>

<%
If request. Form ("Submit") = "change" then
C_title = request. Form ("c_title ")
C_content = request. Form ("c_content ")
C_id = request. Form ("c_id ")
C_filepath = request. Form ("c_filepath ")

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From c_news where c_id =" & c_id
Rs. Open SQL, Conn, 3, 2
RS ("c_title") = c_title
RS ("c_content") = c_content
RS ("c_time") = now ()
Rs. Update
Rs. Close
Set rs = nothing
%>

<% ''Open the template code and convert the special code to the accept value.
Sql1 = "select m_id, m_html from c_moban where m_id = 1"
Set RS1 = server. Createobject ("ADODB. recordset ")
Rs1.open sql1, Conn, 1, 1
Mb_code = RS1 ("m_html ")
Rs1.close
Set RS1 = nothing
Conn. Close
Set conn = nothing
C_title = htmlencode (c_title)
C_content = htmlencode (c_content)
Mb_code = Replace (mb_code, "$ cntop $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To generate an HTML page
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set fout = FSO. createtextfile (server. mappath (c_filepath ))
Fout. writeline mb_code
Fout. Close
%>
<% Response. Redirect ("showit. asp") %>
<% End if %>

<%
If id <> "" then
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From c_news where c_id =" & ID
Rs. Open SQL, Conn, 1, 1
C_id = RS ("c_id ")
C_filepath = RS ("c_filepath ")
C_title = RS ("c_title ")
C_content = RS ("c_content ")
End if
%>

<Form action = "change. asp" method = "Post">
Title: <input type = "text" name = "c_title" value = <% = c_title %> <br>
Content: <br>
<Textarea name = "c_content" rows = "8" Cols = "30"> <% = c_content %> </textarea> <br>
<Input type = "Submit" value = "change" name = "Submit">
<Input type = "reset" value = "reset">
<Input name = "c_id" type = "hidden" value = "<% = ID %>">
<Input name = "c_filepath" type = "hidden" value = "<% = c_filepath %>">
</Form>

8. Delete the record page del. asp.

Same! Delete: In addition to deleting records in database tables, the corresponding HTML page also needs to be deleted. The Code is as follows:

<! -- # I nclude file = "conn. asp" -->

<%
C_id = request. querystring ("c_id ")
SQL = "select * From c_news where c_id =" & c_id
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 2, 3

Filepath = RS ("c_filepath ")
Set FSO = Createobject ("scripting. FileSystemObject ")
FSO. deletefile (server. mappath (filepath ))
Set FSO = nothing

Rs. Delete
Rs. Close
Set rs = nothing
Conn. Close
Set conn = nothing
%>

<% Response. Redirect ("showit. asp") %>

4. Other functions

Template Management page:

It will not always open the database table to add or modify the template code. Therefore, the page program for code management should not be less, and it should be very easy to do it by yourself. Of course, the previous administrator login authentication program will not be described in the book :) also, if multiple templates are designed, you should add a template to select a single region when posting information, similarly, when converting HTML, SQL selects different m_ids to generate html

Method 1: FSO

Set FS = Createobject ("scripting. FileSystemObject ")
Newfile = server. mappath ("/asp/chap06/AT/newfile.html ")
'Create a new file/newfile.html. If the file already exists, overwrite it.
Set a = FS. createtextfile (newfile, true)
Response. write "the new file has been created! "
A. Close
File = server. mappath ("newfile.html ")
Set TXT = FS. opentextfile (file, 8, true) 'open the file into which data can be written at the end
Data1 = "this sentence is written using the writeline method !~~ "
TXT. writeline data1
Data2 = "this sentence is written using the write method !~~ "
TXT. Write data2
TXT. Close

Method 2: XMLHTTP

<%
Set xml = server. Createobject ("Microsoft. XMLHTTP ")
'Replace the following address with the file address of your homepage. You must use an absolute path starting with http: //. You cannot write relative paths.
XML. Open "get", "http://www.phpup.com", false
XML. Send
Bodytext = xml. responsebody
Bodytext = bytestobstr (bodytext, "gb2312 ")
Set xml = nothing
Dim FSO, myfile
Set FSO = Createobject ("scripting. FileSystemObject ")
Set myfile = FSO. createtextfile (server. mappath ("aa.htm"), true)
Myfile. writeline (bodytext)
Myfile. Close

Others:

1

In the following example, index. asp? Id = 1/index. asp? Id = 2/index. asp? Id = 3/these three dynamics
Ndex1.htm, index2.htm, and index3.htm are stored in the root directory:

<%
Dim strurl, item_classid, ID, filename, filepath, do_url, html_temp
Html_temp = "<ul>"
For I = 1 to 3
Html_temp = html_temp & "<li>"
Item_classid = I
Filename = "Index" & item_classid & ". htm"
Filepath = server. mappath ("/") & "//" & filename html_temp = html_temp & filepath & "</LI>"
Do_url = "http ://"
Do_url = do_url & request. servervariables ("SERVER_NAME") & "/main/index. asp"
Do_url = do_url &"? Item_classid = "& item_classid

Strurl = do_url
Dim objxmlhttp
Set objxmlhttp = server. Createobject ("Microsoft. XMLHTTP ")
Objxmlhttp. Open "get", strurl, false
Objxmlhttp. Send ()
Dim binfiledata
Binfiledata = objxmlhttp. responsebody
Dim objadostream
Set objadostream = server. Createobject ("ADODB. Stream ")
Objadostream. type = 1
Objadostream. open ()
Objadostream. Write (binfiledata)
Objadostream. savetofile filepath, 2
Objadostream. Close ()

Next
Html_temp = html_temp & "<ul>"
%>

<%
Response. Write ("successfully generated file :")
Response. Write ("<br> ")
Response. Write html_temp
%>

Function bytestobstr (body, cset)
Dim objstream
Set objstream = server. Createobject ("ADODB. Stream ")
Objstream. type = 1
Objstream. mode = 3
Objstream. Open
Objstream. Write body
Objstream. Position = 0
Objstream. type = 2
Objstream. charset = cset
Bytestobstr = objstream. readtext
Objstream. Close
Set objstream = nothing
End Function
%>

2

<% @ Language = "VBScript" codePage = "936" %>
<%
Public tempelatefile, tmpdata
Sub ofile () 'open the file and put the file content in tmpdata
On Error resume next
Tmpdata = ""
Set astream = server. Createobject ("ADODB. Stream ")
Astream. type = 2' file type text
Astream. mode = 3' read/write
Astream. Open
Astream. charset = "gb2312" 'Character Set
Astream. loadfromfile (tempelatefile )'
ASSP = astream. Size
If err. Number <> 0 then
Xz =-18
Response. Write tempelatefile & "<br>"
Err. Clear
Tmpdata = ""
Else
Tmpdata = astream. readtext (ASSP)
End if

End sub

Sub save_file ()
Ofile ()
Recfilen = server. mappath (DTS)
Astream. Flush
Astream. Close
Astream. type = 2
Astream. mode = 3
Astream. Open
Astream. charset = "gb2312"
Astream. Position = 0
Astream. writetext tmpdata, 1' write data to stream
Astream. savetofile recfilen, 2' save to file
End sub

Function DTS () 'generates random file names
If Len (month (now ()> 1 then
Mm = month (now ())
Else
Mm = "0" & month (now ())
End if
If Len (Day (now ()> 1 then
D = Day (now ())
Else
D = "0" & Day (now ())
End if
If Len (hour (now ()> 1 then
H = hour (now ())
Else
H = "0" & hour (now ())
End if
If Len (minute (now ()> 1 then
M = minute (now ())
Else
M = "0" & minute (now ())
End if
If Len (second (now ()> 1 then
S = second (now ())
Else
S = "0" & Second (now ())
End if
Randomize
Upper Bound = 9999
Lowerbound = 1000
RDS = int (upperbound-lowerbound + 1) * RND + lowerbound)
DTS = "htm/" & year (now () & mm & D & H & M & S & RDS & ". htm"
End Function
Title = request. Form ("title ")
Content = request. Form ("content ")
Tmpdata = Replace (tmpdata, "<title> </title>", title) 'to support replacement of submitted content
Tmpdata = Replace (tmpdata, "<content> </content>", content)
Tempelatefile = server. mappath ("tempelate/1.htm") 'Template File
Save_file ()
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.