asp產生html靜態檔案的一些方法整理

來源:互聯網
上載者:User

做網站都想用到動態資訊,而動態又慢,那麼就出來了 動態產生靜態方法 了,asp產生HTML,但是發現產生的內容不是最新的,比如我產生首頁,產生後我又修改了動態首頁檔案,在點擊產生,可產生出來的首頁不是動態首頁,也就是說瀏覽動態首頁和靜態首頁的內容不一樣,那麼這裡其實不是你程式的問題,是緩衝在作怪,有的人就說了,加上代碼禁止緩衝,那麼也可以,但是代碼比較多點。下面學習吧介紹個方法簡單的解決ASP產生HTML緩衝問題。

用FSO讀取檔案文字框產生HTML這種方法不建議採用,可以用FSO+XMLHTTP產生,用xmlhttp來擷取指定檔案的內容,用FSO產生,很是方便,那麼解決緩衝在這個擷取的時候,一般都是 絕對URL,在這個URL後面加個隨機數變數。如default.asp?a="&now()&",因為這個a是當前時候,而目前時間不重複,就讓程式認為不是一個檔案,就不會有緩衝出現,也可以保證每次產生的HTML都是最新的。


下面使用的是xmlhttp來擷取動態內容,然後用FSO來組建檔案。

asp產生html代碼:

 代碼如下 複製代碼

dim read,reada,Curl,Curla,content,contenta
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if

getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
On Error Resume Next
if err.number<>0 then err.Clear
End function
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

url1=Request.ServerVariables("Server_name")
Curl="http://"&url1&"/index.asp?a="&now()&""
read=getHTTPPage(Curl)
if read<>"" then
content=read
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.html")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response.Write("已經產生首頁")
end if

以上代碼為asp產生首頁HTML,或者單個檔案HTML。

代碼解釋:

url1 是擷取當前地址欄的網域名稱,比如本地瀏覽的時候為http://127.0.0.1/那麼他就是127.0.0.1

Curl是擷取出來的網域名稱加上你的動態檔案地址,這裡為index.asp後面加個a=now()是為了杜絕緩衝設定的。

Filen是產生到為什麼檔案,這裡是index.html,這個支援相對路徑,比如產生為上一個目錄的html,那麼這樣寫="../index.html"

下次在發表用asp產生列表和內頁HTML,敬請關注


例子,替換方法

如一個正常的index.asp動態網頁面,

建立一個檔案 makeasp2html.asp

 代碼如下 複製代碼

<form method="post" action=""> 
    <textarea name="asp2html" style="display:none"><!--#include file="index.asp"--></textarea> 
    <input type="submit" value="產生html頁"/> 
</form> 

<% 
If request.Form("asp2html")<>"" then 
    filename="index.html" 

    set fso = Server.CreateObject("Scripting.FileSystemObject") 
    set fout = fso.CreateTextFile(server.mappath(""&filename&"")) 

    fout.write request.form("asp2html") 
    fout.close 

    set fout=nothing 
    set fso=nothing 

end if 
%>

這樣index.html檔案就產生了,連模板都用不著,只要伺服器要支援FSO,將正常情況下使用的ASP檔案讀取到textarea裡就可以了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.