ASP.NET 4.8 write memory stream to browser error

54 Views Asked by At

We have an ASP.NET 4.8 project using VB.NET. We recently encounter an error, when exporting a large amount of data to an Excel object by writing memory stream to response, there is a browser error "ERR_SSL_PROTOCOL_ERROR". But when the data size is small, it exports just fine. I have searched Stack Overflow and yet to find a relevant topic.

The export code is:

Dim stream As New MemoryStream()

Using ms As MemoryStream = New MemoryStream()
    Using wb As XLWorkbook = CreateExcelSheet(sArr, sTitle, sSubTitle)
        wb.SaveAs(ms, True, True)
        
        Dim bytesInStream As Byte() = ms.ToArray()
        ms.Close()
        response.Clear()
        response.ContentType = "application/vnd.ms-excel"
        response.AddHeader("content-disposition", "attachment;    filename=" + strExportFileName + ".xlsx")
        response.BinaryWrite(bytesInStream)
        response.End()
    End Using
End Using

The CreateExcelSheet function will create an Excel object, and the code will write it to a memory stream, and then it's written to response.

There is no server error, so it seems to me it's a client side problem. However by inspecting the debug tools I could not find any useful information.

enter image description here

It says "the site" sent an invalid response. And the error is "ERR_SSL_PROTOCOL_ERROR". But again the problem does not exist when the data size is small.

1

There are 1 best solutions below

0
Albert D. Kallal On

Consider adding a flush, see if that helps.

eg:

    response.BinaryWrite(bytesInStream)
    response.Flush()   <--- try adding this
    response.End()