1

Initially, my controller action accepts GET. When my data grew, I was forced to move to POST method to be able to send larger data. My controller action is as follows:

[HttpPost]
public ActionResult ClausesPdf(MyArrayModel obj)
{
    ...
    return File(pdf, "application/pdf", "file.pdf");
}

How can I call this action and download the file using javascript?

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62

1 Answers1

1

Update: I realized that my original answer about AJAX was not entirely accurate since there is no way to return a file from an AJAX call. I suggest that you look at this SO question: Download Excel file via AJAX MVC. I believe @CSL has a good answer that is similar to what you want.

My answer is not plain javascript. This is an AJAX call in jQuery on how it could be done there:

$.ajax({
    url: urlControllerAction,
    type: 'POST',
    cache: false,
    data: //your parameter data here
})
.done(
    function(result, textStatus, jqXHR) {
        //do something with the "result"
    }
)
    .fail(
        //what should be done if something goes wrong
    );

Community
  • 1
  • 1
Hauns TM
  • 1,909
  • 2
  • 22
  • 38