set stream = Server.CreateObject("ADODB.Stream")
stream.mode = 3 '1-读,2-写,3-读写
stream.type = 1 '1-二进制,2-文本
stream.Open
dataStream.position = index
dataStream.CopyTo stream, count
'将另一 Stream 对象 dataStream 从 index 到 index+count 的数据拷贝到 stream
stream.SaveToFile filePath, 2
'将 stream 数据保存为文件,第二个参数:1-不允许覆盖,2-覆盖写入
stream.Close
set stream = nothing
常用属性和方法:
mode 读写模式,可选值:1-读,2-写,3-读写,其它不常用
type 数据类型,可选值:1-二进制,2-文本
charset 编码方式,可选值:ascii,gb2312
position 数据流位置,表示数据操作从这里开始,第一个位置的值为 0,不是 1。
size 数据流大小(字节)
LoadFromFile 从文件读取数据到 Stream 对象,Stream 对象原有内容将被清空
SaveToFile 将 Stream 对象数据保存为文件,第二个参数:1-不允许覆盖,2-覆盖写入
Open 打开数据流
Close 关闭数据流
Read([长度]) 从 Stream 对象中读取二进制数据,不指定长度表示全部读取
ReadText([长度]) 从 Stream 对象中读取文本数据,不指定长度表示全部读取
Write(buffer) 将缓存数据写入 Stream 对象
WriteText(data, [option]) 将文本数据写入 Stream 对象,第二个参数:0-字符写入,1-行写入
CopyTo(destStream, count) 将 Stream 对象的指定数据拷贝到 destStream
'***************************************************
利用ADODB.Stream使用浏览器下载服务器文件..
程序代码:
内容: download.asp?file=相对路径的文件
就可以把这个文件下载下来
call downloadFile(replace(replace(request("file"),"\",""),"/",""))
Function downloadFile(strFile)
' make sure you are on the latest MDAC version for this to work
' -------------------------------------------------------------
' get full path of specified file
strFilename = server.MapPath(strFile)
' clear the buffer
Response.Buffer = True
Response.Clear
'create stream
Set s = Server.CreateObject("ADODB.Stream")
s.Open
'Set as binary
s.Type = 1
'load in the file
on error resume next
' check the file exists
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(strFilename) then
Response.Write("<h1>Error:</h1>" & strFilename & " does not exist<p>")
Response.End
end if
' get length of file
Set f = fso.GetFile(strFilename)
intFilelength = f.size
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
' send the headers to the users browser
Response.AddHeader "Content-Disposition","attachment; filename=" &f.name
Response.AddHeader "Content-Length",intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
' output the file to the browser
Response.BinaryWrite s.Read
Response.Flush
' tidy up
s.Close
Set s = Nothing
End Function
4268

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



