gpt4 book ai didi

c# - 从 byte[] 数组在 ashx 中创建一个 excel 文件以供客户端下载

转载 作者:行者123 更新时间:2023-11-30 18:37:15 25 4
gpt4 key购买 nike

在使用 DataTableReader 从 DataTable 读取数据后,我尝试从 byte[] 数组创建一个 Excel 文件,所有这些都在一个 ashx 文件中。但这就是行不通。我在这里发布一些代码:

DataTableReader RS = dt.CreateDataReader();
byte[] byteArray = GetData(RS);

context.Response.ContentType = "application/ms-excel";
context.Response.Clear();

// context.Response.Charset = "";
try
{
context.Response.BinaryWrite(byteArray);
context.Response.OutputStream.Write(byteArray, 0, byteArray.Length);
context.Response.BufferOutput = true;
context.Response.Flush();
}
catch (Exception ex)
{
SendMail(ex.Message.ToString());
}

它抛出以下异常:

context.Response.SubStatusCode threw an exception of type System.PlatformNotSupportedException. {"This operation requires IIS integrated pipeline mode."} ashx

我知道如果我使用头文件需要有 IIS7 或 Framework 3+。

如有任何帮助,我们将不胜感激!

最佳答案

您设置Response ContentType 然后清除 Response。然后你把回复写了两次,这也很奇怪。我会改变它,我也会尝试调用 End 而不是 Flush

让它工作的最少代码量是这样的:

...
DataTableReader RS = dt.CreateDataReader();
byte[] byteArray = GetData(RS);

try
{
context.Response.Clear();
context.Response.ContentType = "application/ms-excel";
context.Response.OutputStream.Write(byteArray, 0, byteArray.Length);
context.Response.End();
}
catch (Exception ex)
{
SendMail(ex.Message.ToString());
}
...

关于c# - 从 byte[] 数组在 ashx 中创建一个 excel 文件以供客户端下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com