简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM