gpt4 book ai didi

c# - StreamContent 没有加载到最后

转载 作者:行者123 更新时间:2023-11-30 21:37:48 28 4
gpt4 key购买 nike

我有(几个)WebAPI 操作,它们从数据库(通过 EF)加载 QuickFix 日志并使用此私有(private)方法将它们作为 CSV 返回:

private HttpResponseMessage BuildCsvResponse<T>(T[] entries, Func<T, string> row, string fileName)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
var i = entries.Length;
foreach (var entry in entries)
{
i--;
writer.WriteLine(row(entry)); // simply call to overridden ToString() method
}
stream.Seek(0, SeekOrigin.Begin);
stream.Flush();
response.Content = new StreamContent(stream);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName,
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
return response;
}

问题内容永远不会加载到末尾 并且在离末尾不远的随机符号上剪切。为什么会发生?

可能很重要 - 所有日志字符串都包含分隔符 0x01

最佳答案

在接触底层流之前,您需要刷新流编写器的内部缓冲区。

最好是使用另一个 contructor 告诉您的 StreamWriter 保持流打开.然后,您可以安全地处置您的 streamwriter,使其刷新其缓冲区,同时您的 memorystream 实例保持打开状态并且不会被处置。

请注意,您需要选择与您的 HTTP 内容响应相匹配的编码。我在这里选择UTF8,相应地进行调整。

var stream = new MemoryStream();
// notice the true as last parameter, false is the default.
using(var writer = new StreamWriter(stream, Encoding.UTF8, 8192, true))
{
var i = entries.Length;
foreach (var entry in entries)
{
i--;
writer.WriteLine(row(entry)); // simply call to overridden ToString() method
}
}
// your streamwriter has now flushed its buffer and left the stream open
stream.Seek(0, SeekOrigin.Begin);
// calling Flush on the stream was never needed so I removed that.
response.Content = new StreamContent(stream);

关于c# - StreamContent 没有加载到最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46847907/

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