经典的ASP,VBScript上下文。
包括这个微软的在内的许多文章都表示,不能使用FileSystemObject读取Unicode文件。
我在前一段时间遇到了这个问题,因此根据ReadText示例这里,我转而使用FileSystemObject.OpenTextFile (它确实接受一个指示是否以unicode的形式打开文件的最终参数,但实际上无法工作)。
但是,ADODB.Stream在尝试读取UNC文件共享(与权限相关的问题)上的文件时,会造成极大的痛苦。因此,对此进行调查时,我无意中发现了以下方法:( a)使用unicode文件;( b)跨UNC文件程序:
dim fso, file, stream
set fso = Server.CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile("\\SomeServer\Somefile.txt")
set stream = file.OpenAsTextStream(ForReading,-1) '-1 = unicode这是使用FSO来读取unicode文件,没有任何明显的问题,所以我对所有的引用感到困惑,包括MS,因为您不能使用FSO来读取unicode文件。
有其他人使用这种方法读取unicode文件吗?我遗漏了什么隐藏的问题吗?或者您真的可以使用FSO读取unicode文件吗?
发布于 2009-09-11 13:02:57
是的,文件已经过时了。脚本组件在其早期确实经历了一系列更改(如果您使用早期绑定,其中一些更改会中断),但是由于至少WK2000、SP4和XP SP2,它非常稳定。
只是小心你所说的unicode。有时,unicode这个词被更广泛地使用,可以涵盖unicode的任何编码。例如,FSO不读取unicode的UTF8编码。为此,您需要回到ADODB.Stream上。
发布于 2009-09-23 11:21:18
我认为MS没有正式声明它支持unicode,因为:
下面是一些示例代码,我已经成功地使用这些代码(几年来)来使用FSO自动检测和读取unicode文件(假设它们是小endian并包含BOM):
'Detect Unicode Files
Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, False)
intAsc1Chr = Asc(Stream.Read(1))
intAsc2Chr = Asc(Stream.Read(1))
Stream.Close
If intAsc1Chr = 255 And intAsc2Chr = 254 Then
OpenAsUnicode = True
Else
OpenAsUnicode = False
End If
'Get script content
Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, 0, OpenAsUnicode)
TextContent = Stream.ReadAll()
Stream.Close发布于 2012-11-08 14:18:54
'assume we have detected that it is Unicode file - then very straightforward
'byte-by-byte crawling sorted out my problem:
'.
'.
'.
else
eilute=f.ReadAll
'response.write("ČIA BUVO ČARLIS<br/>")
'response.write(len(eilute))
'response.write("<br/>")
elt=""
smbl=""
for i=3 to len(eilute) 'First 2 bytes are 255 and 254
baitas=asc(mid(eilute,i,1))
if (i+1) <= len(eilute) then
i=i+1
else
exit for
end if
antras=asc(mid(eilute,i,1))*256 ' raidems uzteks
'response.write(baitas)
'response.write(asc(mid(eilute,i,1)))
'response.write("<br/>")
if baitas=13 and antras=0 then 'LineFeed
response.write(elt)
response.write("<br/>")
elt=""
if (i+2) <= len(eilute) then i=i+2 'persokam per CarriageReturn
else
skaicius=antras+baitas
smbl="&#" & skaicius & ";"
elt=elt & smbl
end if
next
if elt<>"" then
response.write(elt)
response.write("<br/>")
elt=""
end if
end if
f.Close
'.
'.https://stackoverflow.com/questions/1410334
复制相似问题