Tôi đã có ấn tượng rằng khi bạn gọi Flush()
trong đối tượng StreamWriter nó ghi vào luồng cơ bản, nhưng dường như điều này không đúng với mã của tôi.StreamWriter ghi vào MemoryStream
Thay vì ghi vào tệp của tôi, nó sẽ không viết gì cả. Bất kỳ ý tưởng mà tôi đang đi sai?
public FileResult DownloadEntries(int id)
{
Competition competition = dataService.GetCompetition(id);
IQueryable<CompetitionEntry> entries = dataService.GetAllCompetitionEntries().Where(e => e.CompetitionId == competition.CompetitionId);
MemoryStream stream = new MemoryStream();
StreamWriter csvWriter = new StreamWriter(stream, Encoding.UTF8);
csvWriter.WriteLine("First name,Second name,E-mail address,Preferred contact number,UserId\r\n");
foreach (CompetitionEntry entry in entries)
{
csvWriter.WriteLine(String.Format("{0},{1},{2},{3},{4}",
entry.User.FirstName,
entry.User.LastName,
entry.User.Email,
entry.User.PreferredContactNumber,
entry.User.Id));
}
csvWriter.Flush();
return File(stream, "text/plain", "CompetitionEntries.csv");
}
Cân nhắc đặt MemoryStream và StreamWriter của bạn trong việc sử dụng báo cáo cho thu gom rác thải thích hợp. – neontapir
@neontapir Tôi chưa làm tròn để dọn dẹp mã nhưng sẽ sau đó. Chúc mừng – ediblecode
@neontapir, bình luận sai cho trường hợp cụ thể này: điều này sẽ chỉ làm cho vấn đề nghiêm trọng hơn/hiển nhiên ... –