用ADODB.Stream代替FSO读取文本文件
昨天我问过这个问题,不过没人回答到点上,今天搞定了.贴出来给觉得有用的朋友,希望其他朋友不用走弯路.
1'文件内容读取.
2Function LoadFile(ByVal File)
3 Dim objStream
4 On Error Resume Next
5 Set objStream = Server.CreateObject("ADODB.Stream")
6 If Err.Number=-2147221005 Then
7 Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
8 Err.Clear
9 Response.End
10 End If
11 With objStream
12 .Type = 2
13 .Mode = 3
14 .Open
15 .LoadFromFile Server.MapPath(File)
16 If Err.Number<>0 Then
17 Response.Write "<div align='center'>文件<font color='#ff0000'>"&File&"</font>无法被打开,请检查是否存在!</font></div>"
18 Err.Clear
19 Response.End
20 End If
21 .Charset = "GB2312"
22 .Position = 2
23 LoadFile = .ReadText
24 .Close
25 End With
26 Set objStream = Nothing
27End Function
28
29'存储内容到文件
30Sub SaveToFile(ByVal strBody,ByVal File)
31 Dim objStream
32 On Error Resume Next
33 Set objStream = Server.CreateObject("ADODB.Stream")
34 If Err.Number=-2147221005 Then
35 Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
36 Err.Clear
37 Response.End
38 End If
39 With objStream
40 .Type = 2
41 .Open
42 .Charset = "GB2312"
43 .Position = objStream.Size
44 .WriteText = strBody
45 .SaveToFile Server.MapPath(File),2
46 .Close
47 End With
48 Set objStream = Nothing
49End Sub
2Function LoadFile(ByVal File)
3 Dim objStream
4 On Error Resume Next
5 Set objStream = Server.CreateObject("ADODB.Stream")
6 If Err.Number=-2147221005 Then
7 Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
8 Err.Clear
9 Response.End
10 End If
11 With objStream
12 .Type = 2
13 .Mode = 3
14 .Open
15 .LoadFromFile Server.MapPath(File)
16 If Err.Number<>0 Then
17 Response.Write "<div align='center'>文件<font color='#ff0000'>"&File&"</font>无法被打开,请检查是否存在!</font></div>"
18 Err.Clear
19 Response.End
20 End If
21 .Charset = "GB2312"
22 .Position = 2
23 LoadFile = .ReadText
24 .Close
25 End With
26 Set objStream = Nothing
27End Function
28
29'存储内容到文件
30Sub SaveToFile(ByVal strBody,ByVal File)
31 Dim objStream
32 On Error Resume Next
33 Set objStream = Server.CreateObject("ADODB.Stream")
34 If Err.Number=-2147221005 Then
35 Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
36 Err.Clear
37 Response.End
38 End If
39 With objStream
40 .Type = 2
41 .Open
42 .Charset = "GB2312"
43 .Position = objStream.Size
44 .WriteText = strBody
45 .SaveToFile Server.MapPath(File),2
46 .Close
47 End With
48 Set objStream = Nothing
49End Sub