我目前正在尝试使用ASP.NET MVC 4生成和下载Excel文件。
文件生成工作正常,但下载的行为是我无法理解的。
当我点击我的下载按钮时:
@Html.ActionLink("DOWNLOAD", "Download", "Session", new { sessionId = Model.Session.Id, clientId = Model.Client.Id }, new { @class = "my-link" })
使用正确的内容打开excel文件,但它保存在D:\data\documents\System.IO.MemoryStream
,这不是我的默认下载目录。
一个空文件保存在D:\downloads\infos.xls
,这是我的默认下载目录。
Directory D:\downloads
18/07/2016 05:10 <DIR> .
18/07/2016 05:10 <DIR> ..
18/07/2016 05:10 0 infos.xls
1 file(s) 0 octets
Directory D:\data\documents
18/07/2016 05:10 <DIR> .
18/07/2016 05:10 <DIR> ..
18/07/2016 05:10 8 521 System.IO.MemoryStream
1 file(s) 8 521 octets
这是我的控制器方法:
using Excel = Microsoft.Office.Interop.Excel;
[Authorize]
public class SessionController : ControllerBase
{
[HttpGet]
public ActionResult Download(int sessionId, int clientId)
{
/* [...] Retreive infos */
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
/* [...] Insert infos in worksheet */
byte[] data;
using (var stream = new System.IO.MemoryStream())
{
workbook.SaveAs(stream);
stream.Position = 0;
data = stream.ToArray();
}
return File(data, "application/vnd.ms-excel", "infos.xls");
}
}
您知道为什么有两个下载文件?
感谢您的帮助。