MVC Javascript Method Call Not Bringing Up Excel Save Dialog
Solution 1:
What am I doing wrong?
In ASP.NET MVC you normally invoke controller actions which return ActionResult, they don't use Response.Write
:
public ActionResult Export(InvoicesAndId invoicesAndId)
{
Response.ContentType = "application/ms-excel";
byte[] excel = Encoding.UTF8.GetBytes("word"); // Obviously not a valid Excel
return File(excel, "application/ms-excel", "invoices.csv");
}
Now obviously setting content type to "application/ms-excel"
and writing a simlpe string such as word
to the response, don't expect to get anything but a corrupt Excel file. But I guess that was just an example that you will fix and write a real Excel file.
Or adjust your content type to CSV if this is what you intend to send:
Response.ContentType = "text/csv";
Now that's only the first part. The second part and the one that's actually at the root of your problem is that you are using AJAX to invoke this controller action. You cannot use AJAX to invoke controller actions that are supposed to download files. Simply because when you use AJAX to invoke such an action you end up in the success callback with the data
javascript variable passed to it and containing the CSV file. Obviously since you are now on the client side you cannot do anything with it. For security reasons you cannot save it to the client nor prompt for download (it's already downloaded).
So you should use standard links or form submissions to invoke controller actions that are supposed to stream files to be downloaded.
Solution 2:
You don't have to use $.ajax, you can serialize your parameters with $.param()
and set location to constructed url;
var InvoicesAndId = { values: indexArray, Id:3 };
location.href = 'Invoice/Export?' + $.param(InvoicesAndId, true);
This way, browser will try to navigate to new url, but because it returns a file, file open/save dialog will appear and user will not navigate to another url.
Solution 3:
public class ExcelResult : ActionResult
{
public string FileName { get; set; }
public string Path { get;set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Buffer = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
context.HttpContext.Response.ContentType = "application/vnd.ms-excel";
context.HttpContext.Response.WriteFile(context.HttpContext.Server.MapPath(Path));
}
}
and
public ExcelResult GetExcelFile()
{
return new ExcelResult
{
FileName = "sample.xls", Path = "~/Content/sample.xls"
};
}`
Post a Comment for "MVC Javascript Method Call Not Bringing Up Excel Save Dialog"