我已经查看了所有类似的问题,除非我错过了什么,否则他们不会回答我的问题;所以这里是:
我想在我的网页上打开一个文本文件;但标题是传递的变量。我在尝试使用该变量时遇到错误。这是我的代码:
Option Explicit
Response.CodePage=65001
Response.CharSet="UTF-8"
DIM VTITLE
VTITLE = REQUEST("TITLE_")&".TXT"
Const Filename = VTITLE 'file to read in this case it is 101.txt
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")
' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
' Get a handle to the file
Dim file
set file = FSO.GetFile(Filepath)
' Get some info about the file
Dim FileSize
FileSize = file.Size
Response.Write "<p><b>File: " & Filename & " (size " & FileSize &_
" bytes)</b></p><hr>"
Response.Write "<pre>"
' Open the file
Dim TextStream
Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
' Read the file line by line
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline
' Do something with "Line"
Line = Line & vbCRLF
Response.write Line
Loop
Response.Write "</pre><hr>"
Set TextStream = nothing
Else
Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"
End If
Set FSO = nothing
错误是.. 预期的文字常量
/_READING TEXTFILE.asp,第 8 行
Const Filename = VTITLE '要读取的文件
我尝试了不同的变量方式,例如 &VTITLE 等。
如果我将标题放在“”中,例如“101.txt”,则代码可以工作。 有人可以帮我解决这个问题吗?我猜我不知道如何将变量变成常量。
正如 VBScript 文档 所说;
不能在常量声明中使用变量、用户定义函数或内部 VBScript 函数(例如 Chr)。根据定义,它们不能是常量。
因此,您要么切换到使用已经声明的
VTITLE
变量,要么将 Const Filename
替换为;
Dim Filename: Filename = VTITLE