首页  RSS订阅  繁體中文登录  注册  用户中心

您的位置:ASP教程

  • ASP教程
  • 使用Adodb.Stream下载大文件(2009-11-05)

    使用Adodb.Stream下载文件,如下代码

    程序代码
    Response.ContentType = "application/octet-stream"
    response.AddHeader "Content-Disposition","attachment;filename=" & objRs("Attach_Name")& filetype
    Set Stream = Server.CreateObject("ADODB.Stream")
    Stream.type=1
    Stream.Open
    Stream.LoadFromFile server.mappath(objRs("Attach_path"))
    Response.BinaryWrite Stream.Read
    Stream.Close
    Set Stream = Nothing



    当下载文件小于4M时没问题,大于4M时出错,提示 :

    Response对象错误 'ASP 0251 : 80004005'     
        
    超过响应缓冲区限制

    问题出在IIS的配制中AspBufferingLimit只有4M,所以有如下几种解决办法:

    1、修改IIS的配制

    停止IIS服务,然后用记事本修改C:\WINDOWS\system32\inetsrv\MetaBase.xml 中的:   
       AspBufferingLimit="4194304"    //即默认4M   
      改为:   
       AspBufferingLimit="8388608"    //改为8M或更多,自己决定

    2、如果不想修改IIS的默认配制就要修改代码了。

    程序代码
    Response.Buffer=False ‘将Response.Buffer设为否
    Response.ContentType = "application/octet-stream"
    response.AddHeader "Content-Disposition","attachment;filename=" & objRs("Attach_Name")& filetype
    Set Stream = Server.CreateObject("ADODB.Stream")
    Stream.type=1
    Stream.Open
    Stream.LoadFromFile server.mappath(objRs("Attach_path"))
    offset = 0
    ChunkSize = 2048*1024 ‘ChunkSize小于IIS配制文件中的AspBufferingLimit项所设置的大小
    TotalSize = Stream.Size
    while offset < TotalSize
    if (TotalSize - offset < ChunkSize) then
    ChunkSize = TotalSize – offset
    end if
    strChunk = Stream.Read(ChunkSize)
    Response.BinaryWrite strChunk
    offset = offset + ChunkSize
    wend
    Stream.Close


    或者

    程序代码
    Response.Buffer= true ‘使用缓冲区
    Response.Clear
    Response.ContentType = "application/octet-stream"
    response.AddHeader "Content-Disposition","attachment;filename=" & objRs("Attach_Name")& filetype
    Set Stream = Server.CreateObject("ADODB.Stream")
    Stream.type=1
    Stream.Open
    Stream.LoadFromFile server.mappath(objRs("Attach_path"))
    offset = 0
    ChunkSize = 2048*1024
    TotalSize = Stream.Size
    while offset < TotalSize
    if (TotalSize - offset < ChunkSize) then
    ChunkSize = TotalSize – offset
    end if
    strChunk = Stream.Read(ChunkSize)
    Response.BinaryWrite strChunk
    response.flush ‘这句很重要,它把缓冲区的数据据推出去
    offset = offset + ChunkSize
    wend
    Stream.Close

    上一篇:简简单单教你架设内网专用DNS服务器
    下一篇:分析那些百度长期不收录的黑名单页面