2. 使用FileExists方法 在允许用户访问之前,可以使用FileExists方法检查某些文件是否存在于服务器中(注意这个方法和 FileSystemObject.FileExists以同样的方式工作)。 下面的例子中,用户提供了网页的相对URL,如果用户想通过在名为txtURL的文本框中键入URL打开网页,在重新定向之前可以检查其是否存在。 <% // 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方法的结果 把页面的所有控件放在<FORM>中,提交回本页面,这已经成为一种规范。在页面的开始,查看点击了哪个按钮。如果是FileExists的按钮,就调用组件的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页面中,有时需要一个随机数来完成某些任务,例如,把用户重新定位到一个随机网页、选择颜色或显示每日提示。可以使用VBScript中的Rnd函数,但要把所得数值转变成指定范围内的整数。Tools组件的Random方法更易于使用,因为能够直接提供整数值。 Random方法的结果是一个在-32768~32767范围中的整数值,为了获得一个指定范围的整数,可以使用脚本语言中的Abs函数并对下一个最大的整数取模。例如为了用VBScript语言创建0~20的正整数,可以使用下列语句: intRandom = Abs(objTools.Random) Mod 21 为了得到在50~100之间的数值,可以用: intRandom = (Abs(objTools.Random) Mod 51) + 50 示例网页使用这项技术生成随机数时,首先需要检查由用户输入的数值,以保证这些数值既是有效正整数又有正确的相对关系。 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 当页面重新调入时,结果显示在网页的顶部,如图6-18所示:
图6-18 运行Random方法的结果 4.使用Tools.ProcessForm方法 Tools组件中最复杂的方法是ProcessForm,用来读取存在磁盘上的临时文件,并在其中插入创建的信息(可能来自当前页面的Request.Form集合的内容),然后把结果作为一个文件输出到磁盘,这个方法的语法是: ProcessForm (output_url, template_url, [insertion_point]) 临时文件和输出文件相对于当前页面使用相对URL来定义。输出文件可以是ASP网页,如果这样,当其在浏览器打开时,将正常处理。临时文件可以包含普通的ASP代码,但不运行,仅简单地拷贝输出文件。然而,如果把临时文件中的ASP代码放在<%%…%%>限定符中,当临时文件调入时代码将被执行,这允许动态生成的数值(诸如进行处理的时间和日期)插入到输出页面中。 下面是示例文件template.asp(在chapter06目录的Tools子目录中): This file was created by the ASP Tools component ------------------------------------------------ The content of the request was:
------------------------------------------------ Created <%% = Now() %%> 示例页面包含着预定使用这个临时文件的控件,这些控件创建一个和临时文件在同一个文件夹中的名为output.asp的输出文件,如图6-19所示:
图6-19 运行Tools.ProcessForm方法的界面 点击按钮时,将运行一部分ASP代码,从文本框中采集数据并调用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) 设置输出文件的访问权限 如果得到一个“MSWC.Tools error 80004005 Couldn’t open output file”错误信息,这意味着IIS不允许向指定的目录中写入输出文件。解决这个问题最快捷的方法是,在Properties对话框中的Security选项卡中,将相应的Tools目录以及存放output.asp的目录的Everyone组的Full Control设置成允许,如图6-20所示:
图6-21 输出文件的内容 这里省略了显示来自前面见过的文件内容的程序代码。显示内容的方法与ASP Installable Components主菜单页面中用于显示内容链接列表文件的方法相同。使用FileSystemObject和TextStream对象的实例把整个文件读入到一个字符串中,然后将其插入网页中(记住要使用Server.HTMLEncode方法,以便把字符“<”和“>”转换成可用于显示的字符)。 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:"