I am writing an action controller in ASP.NET MVC4 which creates a .xls file and returns that FileStreamresult instance. On client side, I have an anchor tag with href attribute set to the above mentioned action controller. I also need to pass some data to the controller based on which controller with query the database and put data in the file. This data is dynamic based on selections made by user on UI. So, I tried passing data using 'data-json' attribute but parameter values are received as null in action method. If I use $.ajax from client side, browser does not show any prompt to download the file.
**Client side code snippet:**
$('#mydiv').append('<a id="Download" href="/MyController/Download">Download</a>');
$('#mydiv').attr('data-json', userFilters);
**Server side code snippet:**
public ActionResult Download(Queryparam filter)
{
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream);
var line = "test"; //This is just to keep it simple, actual code uses filter o query db
streamWriter.WriteLine(line);
streamWriter.Flush();
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "TestData.xls",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel");
}
userFilters has data in JSON format having same structure as expected by the input parameter in action method. Please share if you have any inputs on how can this be accomplished.
-Thanks