我正在嘗試爲某些數據創建CSV導出。看起來很簡單,並且在Firefox和Chrome中運行得非常好,但在Internet Explorer中,我只是收到一條消息,說該文件無法下載。沒有其他錯誤消息,Visual Studio中沒有中斷,沒有我可以找到的調試信息。從MVC3導出CSV文件 - Internet Explorer問題
這是我的代碼。也許我做錯了什麼?
public ActionResult ExportStudentsCSV(IEnumerable<Student> students) {
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, System.Text.Encoding.UTF8);
writer.WriteLine("Username,Year Level,School Name,State,Date Joined");
foreach (Student student in students) {
writer.WriteLine(
"\"" + student.username
+ "\",\"" + student.year_level
+ "\",\"" + student.SchoolName
+ "\",\"" + student.state
+ "\",\"" + student.join_date
+ "\""
);
}
writer.Flush();
output.Seek(0, SeekOrigin.Begin);
return File(output, "text/csv", "Students_" + DateTime.Now.ToShortDateString().Replace('/', '-') + ".csv");
}
而且我調用這個函數在我的控制器:
return ExportStudentsCSV(model.StudentReport.StudentList);
@qes好點,感謝 – 2012-02-27 00:09:33