0

I have a form that posts data through JQuery to a Controller that then returns a FileResult. I have an href with JQuery wired up that successfully posts to the controller. The FileResult is generated but the client doesn't seem to receive it. What is the standard approach for this?

1 Answer 1

3

Even if you were able to receive the file in javascript what would you do with it? Save it to the client computer? You can't. It doesn't really make sense requesting controller actions which return files using AJAX. Just create a standard link and leave the user decide what to do with the downloaded file.

All you need is to set the proper HTTP headers:

public ActionResult Download()
{
    return File(@"c:\foo.pdf", "application/pdf", "foo.pdf");
}

And then generate your download link:

<%= Html.ActionLink("download file", "download") %>
Sign up to request clarification or add additional context in comments.

4 Comments

That makes sense. When they click download file I do not want to trigger a full page postback. Is this possible by opening another window?
To clarify, the file is created based on the posted data. It is not a static file.
@Kenoyer, if you set the response headers properly it will not navigate to that page.
@@Kenoyer, please see my update which illustrates how you could return a file which will automatically prompt the user for saving it without navigating away from the current page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.