'Send Excel file to Browser Asp.net

I have been breaking my head for the last two days and can't get this to work. I have a report, which I am exporting to Excel and saving on the server file directory. That part works perfectly, I can see the file, and open it, all good. The issue relies on when I try to send it to the browser for download from the server. It does not download anything, I do see the Response Header with all the info on the browser but nothing happens.

Here's one of my many tries

    Private Sub dxgvPOAll_ToolbarItemClick(source As Object, e As ASPxGridViewToolbarItemClickEventArgs) Handles dxgvPOAll.ToolbarItemClick

        Select Case e.Item.Name
            Case "ExportAll"

                Using report As DevExpress.XtraReports.UI.XtraReport = New POs_Dashboard_Report()
                    Using ms As New System.IO.MemoryStream()                        

                        report.Name = "Backorder Tickets"
                        report.ExportOptions.PrintPreview.SaveMode = DevExpress.XtraPrinting.SaveMode.UsingSaveFileDialog

                        Dim xlsxExportOptions As New DevExpress.XtraPrinting.XlsxExportOptions() With {.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile, .ShowGridLines = True, .FitToPrintedPageHeight = True}
                       

                        Dim folderPath As String = "~/Account/TemporaryFiles/" & Utilities.RetrieveEmployeeNumber() & "/" & Guid.NewGuid.ToString & "/"

                        Dim serverPath As String = Server.MapPath(folderPath)

                        Dim xlsxExportFilePath As String = serverPath & report.Name & ".xlsx"

                        Dim folderInf As New DirectoryInfo(serverPath)
                        If Not folderInf.Exists Then folderInf.Create()

                        report.ExportToXlsx(xlsxExportFilePath, xlsxExportOptions)



                        Try

                            Dim objRequest As FileWebRequest = CType(WebRequest.Create(xlsxExportFilePath), FileWebRequest)
                            Dim objResponse As FileWebResponse = CType(objRequest.GetResponse(), FileWebResponse)
                            Dim bufferSize As Integer = 1
                            Response.Clear()
                            Response.ClearHeaders()
                            Response.ClearContent()
                            Response.AppendHeader("Content-Disposition:", "attachment; filename=" & xlsxExportFilePath)
                            Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString())
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                            Dim byteBuffer As Byte() = New Byte(bufferSize + 1 - 1) {}
                            Dim memStrm As MemoryStream = New MemoryStream(byteBuffer, True)
                            Dim strm As Stream = objRequest.GetResponse().GetResponseStream()
                            Dim bytes As Byte() = New Byte(bufferSize + 1 - 1) {}

                            While strm.Read(byteBuffer, 0, byteBuffer.Length) > 0
                                Response.BinaryWrite(memStrm.ToArray())
                                Response.Flush()
                            End While                         
                          

                            HttpContext.Current.Response.Flush()
                            HttpContext.Current.Response.SuppressContent = True
                            HttpContext.Current.ApplicationInstance.CompleteRequest()

                        Catch e1 As ThreadAbortException
                        Catch ex As Exception

                        End Try 
                    End Using
                End Using
        End Select
    End Sub

Here's what I see on the browser's Response-Header:



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source