为什么使用DevExpress导出数据到文件时会抛出zlibeexception

本文关键字:文件 zlibeexception 数据 DevExpress 为什么 | 更新日期: 2023-09-27 18:02:44

我有一些sql表与动态列,我想把它们导出到xlscsv文件使用DevExpress ASPxGridViewExporterASPxGridView当用户点击Get Data按钮在我的Report.aspx页。

  • 用真实数据正确制作表
  • 页面加载成功并真实显示数据

My Problem

当用户单击按钮时,它会下载一个带有以下内容的损坏文件,

我搜索了googlestackoverflowmicrosoft supportdevexpress support和这么多网站,但没有答案!

任何帮助都将不胜感激。

Server Error in '/' Application.        

The stream state of the underlying compression routine is inconsistent.     
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error     and where it originated in the code.       
Exception Details: System.IO.Compression.ZLibException: The stream state of the underlying compression routine is inconsistent.     
Source Error:       
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be     identified using the exception stack trace below.       
Stack Trace:        
[ZLibException: The stream state of the underlying compression routine is inconsistent.]        
   System.IO.Compression.DeflaterZLib.Deflate(FlushCode flushCode) +196     
   System.IO.Compression.DeflaterZLib.ReadDeflateOutput(Byte[] outputBuffer, FlushCode flushCode, Int32& bytesRead) +185        
   System.IO.Compression.DeflaterZLib.System.IO.Compression.IDeflater.GetDeflateOutput(Byte[] outputBuffer) +44     
   System.IO.Compression.DeflateStream.WriteDeflaterOutput(Boolean isAsync) +58     
   System.IO.Compression.DeflateStream.Write(Byte[] array, Int32 offset, Int32 count) +76       
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +578        
   System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders) +1272      
   System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) +191        
   System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size) +88     
   System.IO.Compression.DeflateStream.PurgeBuffers(Boolean disposing) +142     
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +40       
   System.IO.Stream.Close() +26     
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +42      
   System.IO.Stream.Close() +26     
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +767        
   System.Web.HttpResponse.FilterOutput() +127      
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +62       
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +98     
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0         

我的问题

  • 为什么是ZLibException ?

下面是我的代码:

ASP

<%@ Register Assembly="DevExpress.Web.v16.1" Namespace="DevExpress.Web"
    TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.XtraPrinting.v16.1" Namespace="DevExpress.Web"
    TagPrefix="dx" %>
<%@ Register  Assembly="DevExpress.Printing.v16.1.Core, Version=16.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Printing"
    TagPrefix="dx" %>
<asp:Content ID="Content2" ContentPlaceHolderID="C" runat="Server">
    <div id="divMain" class="main" style="padding: 10px">
        <asp:ObjectDataSource ID="ods" runat="server" TypeName="MySite.Control.CustomTable" SelectMethod="ReadTable">
            <SelectParameters>
                <asp:Parameter Type="Int64" Name="ID" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <dx:ASPxGridView ID="grid" runat="server" ClientInstanceName="grid" AutoGenerateColumns="false"
            KeyFieldName="ID" Width="100%">
            <Columns>
                <!-- Columns Will Be Generated Dynamically -->
            </Columns>
            <SettingsCustomizationWindow Enabled="True" />
            <SettingsBehavior ColumnResizeMode="Control" AllowDragDrop="true" AllowFocusedRow="true" />
            <Settings ShowGroupPanel="True" ShowColumnHeaders="true" ShowFilterBar="Visible"
                ShowFooter="true" ShowGroupFooter="VisibleIfExpanded" ShowGroupButtons="true"
                ShowFilterRow="true" ShowFilterRowMenu="true" />
            <SettingsLoadingPanel Mode="ShowOnStatusBar" />
            <GroupSummary>
            </GroupSummary>
        </dx:ASPxGridView>
        <!-- ASPxGridViewExporter -->
        <dx:ASPxGridViewExporter ID="exporter" runat="server" GridViewID="grid">
        </dx:ASPxGridViewExporter>

        <div style="direction: rtl">
            <asp:DropDownList ID="exportType" runat="server">
                <asp:ListItem Text="XLS" Value="XLS" />
                <asp:ListItem Text="CSV" Value="CSV" />
            </asp:DropDownList>
            <asp:Button ID="btnExport" runat="server" Text="Get Data" OnClick="BtnExportClick" />
        </div>
    </div>

</asp:Content>
c#

protected void BtnExportClick(object sender, EventArgs e)
{
    grid.SettingsDetail.ExportMode = GridViewDetailExportMode.All;
    switch (exportType.SelectedValue)
    {
        case "XLS":
            exporter.WriteXlsToResponse();
            break;
        case "CSV":
            exporter.WriteCsvToResponse();
            break;
        default:
            return;
    }
}

为什么使用DevExpress导出数据到文件时会抛出zlibeexception

我不知道你是否能够解决这个问题,但在我的情况下,这是由于Telerik压缩(Rad压缩),它试图压缩输出流。一旦我在类声明中去掉它,问题就消失了。

另一个建议是将网格控件移出更新面板。如果控件位于更新面板中,WriteToResponse方法将不起作用。