I'm currently trying to generate and download an Excel file with ASP.NET MVC 4.
The file generation works fine, but the download have a behavior that I don't understand.
When I click on my download button:
@Html.ActionLink("DOWNLOAD", "Download", "Session", new { sessionId = Model.Session.Id, clientId = Model.Client.Id }, new { @class = "my-link" })
The excel file open with the right content, but it is saved at D:\data\documents\System.IO.MemoryStream
which is not my default download directory.
And a empty file is saved at D:\downloads\infos.xls
, which is my default download directory.
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
Here is my controller method:
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");
}
}
Why are there are two downloaded files?