我正在使用HttpResponse.BinaryWrite
创建下载csv文件。但是我总是在下载的csv中缺少最后一行。下面我提到了我的代码。我尝试在StreamWriter
中添加bufferSize,但似乎没有任何效果。我无法解决问题所在。
public void GetDetailReprot(string groupCode, List<CustomCodeReport> userResponse)
{
using (MemoryStream memoryStream = new MemoryStream())
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + groupCode + "." + CommonConstant.CsvFormat);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.BufferOutput = true;
using (var writer = new StreamWriter(memoryStream, Encoding.UTF8))
{
int count = 0;
writer.WriteLine("TestTakerIdentifier,TestTakerName,Block Name,Item Name,Stimulus,PsuedoWord,UserAnswer,Score");
if (userResponse!= null)
{
foreach (var itemResponse in userResponse)
{
writer.WriteLine("=\"" + itemResponse.TesrTakerId + "\","
+ itemResponse.UserName + ","
+ itemResponse.BlockName + ","
+ itemResponse.ItemName + ","
+ itemResponse.ItemStimulus + ","
+ itemResponse.Score);
count++;
}
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.End();
}
}
}
请帮助!
PS:我尝试使用writer.Flush()
,但仍无效。并且,userResponse
列表是否有100行或500行或1000行,我只丢失了下载的4-5行。那么这个缓冲区如何影响这里?
StreamWriter
此处使用的缓冲区总是只比几个字节少 我需要什么?