Domanda

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")));
    }
È stato utile?

Soluzione

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.

Altri suggerimenti

Response.AddHeader("content-disposition", "attachment;filename=ServiceTrackerData.xslx")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top