<html>
<head>
<http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<% LANGUAGE = VBScript %>
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim filename
filename = "test.txt" '缺省相对路径是c:winnt
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filename)
Set readf = f.OpenAsTextStream(ForReading,TristateFalse)
'第一个参数可选。输入/输出模式,是下列三个常数之一:
' ForReading=1只读、ForWriting=2 可读写或 ForAppending=3追加
'第二个参数也为可选。三个 Tristate 值之一,
' 指出以何种格式打开文件。忽略此参数,则文件以 ASCII
'格式打开。 TristateUseDefault=-2 以系统默认格式打开文件、
'TristateTrue=-1 以 Unicode 格式打开文件或TristateFalse=0
'以 ASCII 格式打开文件。
'也可用OpenTextFile方法打开文件
s = readf.ReadLine
Do While readf.AtEndOfLine <> True
s = readf.ReadLine
Response.write s & "" '逐行读文件并写出
Loop
readf.close
%>
</body>
</html>
<head>
<http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<% LANGUAGE = VBScript %>
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim filename
filename = "test.txt" '缺省相对路径是c:winnt
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filename)
Set readf = f.OpenAsTextStream(ForReading,TristateFalse)
'第一个参数可选。输入/输出模式,是下列三个常数之一:
' ForReading=1只读、ForWriting=2 可读写或 ForAppending=3追加
'第二个参数也为可选。三个 Tristate 值之一,
' 指出以何种格式打开文件。忽略此参数,则文件以 ASCII
'格式打开。 TristateUseDefault=-2 以系统默认格式打开文件、
'TristateTrue=-1 以 Unicode 格式打开文件或TristateFalse=0
'以 ASCII 格式打开文件。
'也可用OpenTextFile方法打开文件
s = readf.ReadLine
Do While readf.AtEndOfLine <> True
s = readf.ReadLine
Response.write s & "" '逐行读文件并写出
Loop
readf.close
%>
</body>
</html>
此博客展示了使用VBScript结合HTML读取文件的代码示例。通过创建文件系统对象,以指定模式和格式打开文件,逐行读取文件内容并输出。代码中详细说明了参数的可选值及含义,如输入/输出模式和文件打开格式等。

4218

被折叠的 条评论
为什么被折叠?



