0

I want to get a FileResult by clicking the button. When I press the button the file is read well, but nothing happens. This is my code,

$(document).ready(function () {
    $('#resource_btn').click(function () {
        $.ajax({
            url: "/Download/DownResource",
            type: "POST",
            cache: false,
            success: function (data) {
                // ?
            },
            error: function () {
                alert("error");
            }
        });
    });
});
[HttpPost]
public ActionResult DownResource() {
    string fileName = Path.Combine(Server.MapPath("~/images/"), "down_arrow.png");
    return File(fileName, "Iamge/png");  // Is that all?
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
lcy
  • 79
  • 9

1 Answers1

0

The best way would be to send the request directly from form, not by JS. You can make to form to post it directly, not by JA and ajax.

Or from JS you can change the accepted method to GET and send it via window.location.

However there are workarounds to force download from JS, but it is not well supported in older browsers. You can get more at: Create a file in memory for user to download, not through server

Community
  • 1
  • 1
Zapo
  • 211
  • 1
  • 6