I am returning File result from action triggered by the form post event. I can't get download dialog. Instead, if I use:
return File(Encoding.UTF8.GetBytes(reportPath), "text/plain", "Report.csv");
I get path to the file upon ajax execution in the target div.
When I use
return File(reportPath, "text/plain", "Report.csv");
I get content of the file in the target div.
The action is declared as
[HttpPost]
public virtual ActionResult ExportFilter(Model model) {
string outputFile = CreateReport(model);
return File(....)
}
The form is submited via Ajax.BeginForm(...).
EDIT
A bit more info: My form has 2 submit buttons. One is used to present result in the target div, the other one is used to export the result. The actton is the same and I use this to determine which button triggered the event:
[HttpPost]
public virtual ActionResult Run( model )
{
var bExecute = !string.IsNullOrEmpty(Request.Form["execute"]);
return bExecute ? Execute(model) : Export(model);
}
[HttpPost]
public virtual ActionResult Execute( model )
{
....
return PartialView("Report", model);
}
[HttpPost]
public virtual FileResult Export( model ) {
.....
return File(....)
}
After some answers I tried to redirect to Get action using :
....
return RedirectToAction( MVC.Report.OfferDownload(ReportFile) );
}
[HttpGet]
public virtual FileResult OfferDownload(string FileName)
{
return File(FileName, "text/csv", "Report.csv");
}
However, that didn't help.
I also tried to Stream file, but it didn't help too.
EDIT2
Definitely Ajax problem cuz it works when I replace Ajax.BeginForm with Html.BeginForm. One solution I have in mind is to use onclick event on submit buttons to change form attributes.