您现在的位置: Gufang > 博客 > 学无止境 > 正文
Asp 读取任意文件,二进制数据流方式下载

<%
Set Fs = Server.CreateObject("Scripting.FileSystemObject") Fso对象
Set Ado = Server.CreateObject("Adodb.Stream") Ado对象
i = 0    计数器
r = 1024 每次读取大小(byte)
FilePath = Server.MapPath("test.mp3") 文件路径
Ado.Mode = 3 1 读,2 写,3 读写。
Ado.Type = 1 1 二进制,2 文本。
Ado.Open
Ado.LoadFromFile(FilePath) 载入文件
    Response.AddHeader "Content-Disposition", "attachment; filename=" & Fs.GetFile(FilePath).name 文件名
    Response.AddHeader "Content-Length", Ado.size 通知浏览器接收的文件大小
    Response.ContentType = "application/octet-stream" 通知浏览器接受的文件类型(可自己定义,很多种,但一般都用这个.)
    While i < Ado.Size 循环读取直到读完为止
        Response.BinaryWrite Ado.Read(r) 输出二进制数据流
        Response.Flush 立即发送(要求至少256字节),不加的话可能提示超过缓存区。
        i = i + r 累加计数器
    Wend
Ado.Close 关闭文件对象
Response.End
%>

    注释基本都写全了,不再解释了。兼容性比较好,支持任意文件。不知道干什么用的?是下载服务器上文件用的,读取服务器任意文件后,采用二进制数据流的方式下载。

发表评论(0)
姓名 *
电子邮件
QQ
评论内容 *
验证问题 * 江苏省的省会是哪个城市?请填写正确答案
验证码 * 图片看不清?点击重新得到验证码