rajeemcariazo
rajeemcariazo

Reputation: 2524

Download a File by POST (HttpPost action) on ASP.NET MVC controller action using Javascript

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?

Upvotes: 1

Views: 12407

Answers (1)

Hauns TM
Hauns TM

Reputation: 1959

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
    );

Upvotes: 1

Related Questions