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 ()
%>