adodb.stream 能直接读字符串吗?一定要生成一个文件,然后再读,读完再删除?

quicklyonline 2009-09-27 09:46:09
现在我有一个字符串,要读成BYTE[]字节流,通过测试,可以先把字符串写成文件到硬盘,然后用STREAM读这个文件,就成了一个BYTE流了。最后还要删除这个文件。感觉比较笨。、


能不能直接读那个字符串,变成BYTE[]流?哪个大哥帮帮忙
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcwtitxu 2009-09-27
  • 打赏
  • 举报
回复
当然可以了



Function GetBytes(str, cSet)
With CreateObject("ADODB.Stream")
.Mode = 3
.Type = 2 ' 设为文本模式。 1 二进制 2 文本
.Open
.Charset = cSet ' 指定读写使用的字符集
.WriteText str ' 写入文本
.Position = 0
.Type = 1 ' 改成二进制模式   需注意的是:切换Type时,Position必须是0
GetBytes = .Read(-1) ' 读出 byte 数组
.Close
End With
End Function


Dim str, bytes
str = "ab中国人"

bytes = GetBytes(str, "GBK")


MsgBox TypeName(bytes)

MsgBox AscB(MidB(bytes, 1, 1)) ' 第一个 Byte
MsgBox AscB(MidB(bytes, 2, 1)) ' 第二个
MsgBox AscB(MidB(bytes, 3, 1)) ' 第三个
hookee 2009-09-27
  • 打赏
  • 举报
回复
如果你是用于下载的,用上面的代码就可以了,不用生成物理文件。
如果你是生成byte[]用于其他目地的,用第三方组件
比如 The Convert Object
http://download.paipai.net/texts/midori-ActiveX-Objects.htm#2.3.2
quicklyonline 2009-09-27
  • 打赏
  • 举报
回复
我不想牵扯到文件,想直接把STR类型,变为BYTE[]类型。
hookee 2009-09-27
  • 打赏
  • 举报
回复

<%
' 数据存入 oStream (ADO Stream) 后
oStream.Position = 0

r = TransferFile(oStream, "application/octec-stream", "xxxx.bin")

Function TransferFile(ByRef oStream, sMimeType, sFilename)
Dim iChar, iSent, iSize
Dim c
Const FILE_TRANSFER_SIZE = 32768 '32k

iSent = 0
TransferFile = True
iSize = oStream.Size
With Response
.Charset = "GB2312"
.ContentType= sMimeType
.AddHeader "Accept-Ranges", "bytes"
.AddHeader "Content-Length", iSize
.AddHeader "Content-Type", sMimeType
.AddHeader "DownloadOptions", "noopen"
.AddHeader "Content-Disposition","attachment; filename=" & sFilename
End With
c = 0
Do While c <= iSize
iChar = oStream.Read(FILE_TRANSFER_SIZE)
c = c + FILE_TRANSFER_SIZE
With Response
.BinaryWrite(iChar)
iSent = iSent + FILE_TRANSFER_SIZE
If (iSent MOD FILE_TRANSFER_SIZE) = 0 Then
.Flush
If Not .IsClientConnected Then
TransferFile = False
Exit Do
End If
End If
End With
Loop
Response.Flush
If Not Response.IsClientConnected Then TransferFile = False
Set oStream = Nothing
End Function
%>
hookee 2009-09-27
  • 打赏
  • 举报
回复
<%
' 数据存入 oStream (ADO Stream) 后
oStream.Position = 0

r = TransferFile(oStream, "application/octec-stream", "xxxx.bin")

Function TransferFile(ByRef oStream, sMimeType, sFilename)
Dim iChar, iSent, iSize
Dim c
Const FILE_TRANSFER_SIZE = 32768 '32k

iSent = 0
TransferFile = True
iSize = oStream.Size
With Response
.Charset = "GB2312"
.ContentType= sMimeType
.AddHeader "Accept-Ranges", "bytes"
.AddHeader "Content-Length", iSize
.AddHeader "Content-Type", sMimeType
.AddHeader "DownloadOptions", "noopen"
.AddHeader "Content-Disposition","attachment; filename=" & sFilename
End With
c = 0
Do While c <= iSize
iChar = oStream.Read(FILE_TRANSFER_SIZE)
c = c + FILE_TRANSFER_SIZE
With Response
.BinaryWrite(iChar)
iSent = iSent + FILE_TRANSFER_SIZE
If (iSent MOD FILE_TRANSFER_SIZE) = 0 Then
.Flush
If Not .IsClientConnected Then
TransferFile = False
Exit Do
End If
End If
End With
Loop
Response.Flush
If Not Response.IsClientConnected Then TransferFile = False
Set oStream = Nothing
End Function
%>

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧