我有(几个)WebAPI操作,它从数据库加载QuickFix日志(通过EF)并使用此私有方法将它们作为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
答案 0 :(得分:3)
在触摸基础流之前,您需要刷新您的streamwriter的内部缓冲区。
最好是告诉您的StreamWriter使用另一个contructor保持流畅通。然后,您可以安全地处理您的streamwriter,使其在您的内存流实例保持打开状态并且不会被处置时刷新其缓冲区。
请注意,您需要选择与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);