IIS7 - Specifying content-length header in ASP causes “connection reset” error

前端 未结 3 666
庸人自扰
庸人自扰 2021-01-12 04:43

I\'m migrating a series of websites from an existing IIS5 server to a brand new IIS7 web server. One of the pages pulls a data file from a blob in the database and serves i

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 05:27

    There are two options to make it work:

    1. Output the "Content-Size" header, instead of "Content-Length". Note not all clients will recognise that, but at least it works.

    2. (Preferred) Set Response.Buffer to True, then you can use the "Content-Length" header, and handle the "chunking" yourself (thus not taxing the ASP memory buffer):

    The following works for me on IIS7, and seems to send file size info correctly to the browser.

    Response.Buffer = True
    Response.ContentType = "application/pdf"
    Response.AddHeader "Content-Disposition", "attachment; filename=""yourfile.pdf"""
    
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile "yourfile.pdf"
    
    Response.AddHeader "Content-Length", objStream.Size
    
    ' Send file in chunks. '
    lByteCount = 0
    lChunkSize = 100000
    While lByteCount < objStream.Size
      If lByteCount + lChunkSize > objStream.Size Then lChunkSize = objStream.Size - lByteCount
      Response.BinaryWrite objStream.Read(lChunkSize)
      Response.Flush ' Flush the buffer every 100KBytes '
      lByteCount = lByteCount + lChunkSize
    Wend
    
    objStream.Close
    Set objStream = Nothing
    

提交回复
热议问题