Reputation: 9812
I'm generating an Excel file using EPPlus and I want the user downloading the file to be prompted to save it rather than have the file immediately open in Excel. How can I do this in MVC?
Here's my current code:
[HttpGet]
public ActionResult DownloadData()
{
byte[] excelData = ExportService.GetServiceTrackerDataAsExcelData();
return File(excelData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", String.Format("ServiceTrackerData-{0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd")));
}
Upvotes: 2
Views: 2782
Reputation: 74899
You can't.
You're telling the browser already that the file is an attachment and therefore the browser will handle it externally. By default most browsers will display a dialog asking to open or save. However, users can choose to set Excel files to open automatically (often a checkbox to 'automatically do this next time'). If the user chose to open automatically in excel, then you can't override that.
Upvotes: 3
Reputation: 6019
Response.AddHeader("content-disposition", "attachment;filename=ServiceTrackerData.xslx")
Upvotes: 0