I am displaying the results on an angular js interface through Web API calls in MVC.
But then, based on some selections, I would need to pass some parameters and call the following MVC Controller method that downloads a file.
public ActionResult Downloadfile(string selectedIds, string selecteddefs, DateTime date)
{
// do some stuff here
var file = new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
FileDownloadName = string.Format("download_{0}.docx", DateTime.Now.ToString("ddMMyyyyHHmmss"))
};
return file;
}
This is a normal MVC Controller which does not inherit from ApiController.If I make the http post like below in the angular function, it's not downloading file thought it hits the controller method. This is probably, it's returning json data rather than downloading a file.And I should not do this I think, since the Controller is a normal MVC one, does not inherit ApiController.
$http.post('/ControllerName/MethodName?selectedIds=' + selectedIds + '&selecteddefs=' + selecteddefs + '&date=' + date, {});
So I tried the normal jquery like below.
$.ajax({
url: "/ControllerName/ActionName",
data: { 'selectedIds': selectedIds.toString(), 'selecteddefs': selecteddefs.toString(), 'date': date },
type: "POST"
});
The above hits the Controller method and does all the work, but then again not able to download any file.
How can I call a normal MVC Controller Action method where the functionality of the method is just to download a file. There is no problem with Action method. Could anyone please help.
I have tried the following.But it does not hit the controller Action.
$http({
method: 'POST',
url: '/Controller/ActionName',
data: $httpParamSerializerJQLike({ nodeIds: JSON.stringify(nodes.toString()), glossaryTermIds: JSON.stringify(glossaryterms.toString()), date: JSON.stringify(date.toString()) }),
headers: { 'Content-Type' : 'application/x-www-form-urlencoded'}
})
.then(function (result){
}, function (result) {
});