Question

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")));
    }
Était-ce utile?

La solution

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.

Autres conseils

Response.AddHeader("content-disposition", "attachment;filename=ServiceTrackerData.xslx")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top