2. 使用FileExists方法 在允許用戶訪問之前,可以使用FileExists方法檢查某些文件是否存在于服務器中(注意這個方法和 FileSystemObject.FileExists以同樣的方式工作)。 下面的例子中,用戶提供了網(wǎng)頁的相對URL,如果用戶想通過在名為txtURL的文本框中鍵入URL打開網(wǎng)頁,在重新定向之前可以檢查其是否存在。 <% // in Jscript: var objTools = Server.CreateObject('MSWC.Tools'); var strURL = Request.Form ('txtURL'); // collect the page URL they entered if (objTools.FileExists(strURL)) // see if it exists Server.Transfer(strURL) ; // if it does, transfer to it Else & nbsp; ; // or if not display a message Response.Write('Sorry, the page you requested does not exist'); %> 這里提供了一個示例頁面(使用VBScript)來演示組件的三種方法(非 Macintosh),可以從asp Installable Components主菜單中運行,如圖6-16所示:
圖6-17 運行FileExists方法的結(jié)果 把頁面的所有控件放在<FORM>中,提交回本頁面,這已經(jīng)成為一種規(guī)范。在頁面的開始,查看點擊了哪個按鈕。如果是FileExists的按鈕,就調(diào)用組件的FileExists方法并顯示合適的信息。 'look for a command sent from the FORM section buttons If Len(Request.Form("cmdExists")) Then strFile = Request.Form("txtFile") If objTools.FileExists(strFile) Then Response.Write "The file '<B>" & strFile & "</B>' does exist.<P>" Else Response.Write "The file '" & strFile & "' <B>does not</B> exist.<P>" End If End If 3.使用Tools.Random方法 在ASP頁面中,有時需要一個隨機數(shù)來完成某些任務,例如,把用戶重新定位到一個隨機網(wǎng)頁、選擇顏色或顯示每日提示。可以使用VBScript中的Rnd函數(shù),但要把所得數(shù)值轉(zhuǎn)變成指定范圍內(nèi)的整數(shù)。Tools組件的Random方法更易于使用,因為能夠直接提供整數(shù)值。 Random方法的結(jié)果是一個在-32768~32767范圍中的整數(shù)值,為了獲得一個指定范圍的整數(shù),可以使用腳本語言中的Abs函數(shù)并對下一個最大的整數(shù)取模。例如為了用VBScript語言創(chuàng)建0~20的正整數(shù),可以使用下列語句: intRandom = Abs(objTools.Random) Mod 21 為了得到在50~100之間的數(shù)值,可以用: intRandom = (Abs(objTools.Random) Mod 51) + 50 示例網(wǎng)頁使用這項技術生成隨機數(shù)時,首先需要檢查由用戶輸入的數(shù)值,以保證這些數(shù)值既是有效正整數(shù)又有正確的相對關系。 If Len(Request.Form("cmdRandom")) Then intMin = -1 'preset to illegal values and then intMax = -1 'only set if a valid number is entered strMin = Request.Form("txtMinimum") strMax = Request.Form("txtMaximum") If IsNumeric(strMin) Then intMin = CStr(strMin) If IsNumeric(strMax) Then intMax = CStr(strMax) If (intMin >= 0) And (intMax > intMin) Then intRandom = (Abs(objTools.Random) Mod (intMax - intMin + 1)) + intMin Response.Write "Your random value is: <B>" & intRandom & "</B><P>" Else Response.Write "<B>The numbers you entered are not valid.</B><P>" End If End If 當頁面重新調(diào)入時,結(jié)果顯示在網(wǎng)頁的頂部,如圖6-18所示:
圖6-18 運行Random方法的結(jié)果 4.使用Tools.ProcessForm方法 Tools組件中最復雜的方法是ProcessForm,用來讀取存在磁盤上的臨時文件,并在其中插入創(chuàng)建的信息(可能來自當前頁面的Request.Form集合的內(nèi)容),然后把結(jié)果作為一個文件輸出到磁盤,這個方法的語法是: ProcessForm (output_url, template_url, [insertion_point]) 臨時文件和輸出文件相對于當前頁面使用相對URL來定義。輸出文件可以是ASP網(wǎng)頁,如果這樣,當其在瀏覽器打開時,將正常處理。臨時文件可以包含普通的ASP代碼,但不運行,僅簡單地拷貝輸出文件。然而,如果把臨時文件中的ASP代碼放在<%%…%%>限定符中,當臨時文件調(diào)入時代碼將被執(zhí)行,這允許動態(tài)生成的數(shù)值(諸如進行處理的時間和日期)插入到輸出頁面中。 下面是示例文件template.asp(在chapter06目錄的Tools子目錄中): This file was created by the ASP Tools component ------------------------------------------------ The content of the request was:
------------------------------------------------ Created <%% = Now() %%> 示例頁面包含著預定使用這個臨時文件的控件,這些控件創(chuàng)建一個和臨時文件在同一個文件夾中的名為output.asp的輸出文件,如圖6-19所示:
圖6-19 運行Tools.ProcessForm方法的界面 點擊按鈕時,將運行一部分ASP代碼,從文本框中采集數(shù)據(jù)并調(diào)用ProcessForm方法: If Len(Request.Form("cmdProcess")) Then strTemplate = Request.Form("txtTemplate") strOutput = Request.Form("txtOutput") strInsertPoint = Request.Form("txtInsert") '… ' we display the template contents here '…
' process the form contents objTools.ProcessForm strOutput, strTemplate, strInsertPoint
'… ' we display the output file contents here '… End If (1) 設置輸出文件的訪問權(quán)限 如果得到一個“MSWC.Tools error 80004005 Couldn’t open output file”錯誤信息,這意味著IIS不允許向指定的目錄中寫入輸出文件。解決這個問題最快捷的方法是,在Properties對話框中的Security選項卡中,將相應的Tools目錄以及存放output.asp的目錄的Everyone組的Full Control設置成允許,如圖6-20所示:
圖6-21 輸出文件的內(nèi)容 這里省略了顯示來自前面見過的文件內(nèi)容的程序代碼。顯示內(nèi)容的方法與ASP Installable Components主菜單頁面中用于顯示內(nèi)容鏈接列表文件的方法相同。使用FileSystemObject和TextStream對象的實例把整個文件讀入到一個字符串中,然后將其插入網(wǎng)頁中(記住要使用Server.HTMLEncode方法,以便把字符“<”和“>”轉(zhuǎn)換成可用于顯示的字符)。 QUOT = Chr(34) 'double-quote character … 'create a FileSytemObject object to display the template file contents Set objfso = Server.CreateObject("Scripting.FileSystemObject")
Response.Write "The content of the template file <B>" & _ strTemplate & "</B> is:"