How can I call an MVC FileContentResult by Jquery and get it to prompt the user to save on it's return?

export to csv using jquery ajax call in mvc
export to excel using jquery ajax call in mvc
filecontentresult save to file
download file on ajax success
mvc download file ajax
spring mvc jquery ajax file download
filecontentresult in web api
download file with ajax post request via spring mvc

I'm generating a CSV from an MVC 3 website and using a FileContentResult to pass this to the user. This worked great but it took 30 seconds for the csv to generate and therefore 30 seconds before the prompt to save was given to the user.

    public virtual FileContentResult GetSpreadsheet(string id)
    {
        var service = new SpreadsheetService();
        var result = service.GetCSV();
        return File(new System.Text.UTF8Encoding().GetBytes(result.Message), "text/csv", "Report123.csv");
    }

So I thought I'd just call it via JQuery - but this (unsurprisingly!) just dumps the CSV to the page.

            $.post("/Home/GetSpreadsheet/" + id, null, function (data) {
                $('#resultDiv').html(data);
            });

Does anyone know how I'd generate the prompt to save now I've got the data back? Thanks

You can't do that. Forget about that. It is impossible to download files using AJAX. In fact the AJAX call will work, you will hit the server, the server will send the file contents back to the client, the AJAX success callback will be triggered and passed as argument the contents of the file and that's where everything ends for you. Using javascript you cannot, for absolutely obvious reasons, save directly the file to the client computer and you cannot prompt for the Save As dialog.

So instead of using AJAX simply create an anchor:

@Html.ActionLink("download spreadsheet", "GetSpreadsheet", new { id = "123" })

Now if the server set the Content-Disposition header to attachment the browser will prompt the user to download and save the file at some chosen location on his computer.

asp.net mvc 3 How can I call an MVC FileContentResult by Jquery , asp.net mvc 3 How can I call an MVC FileContentResult by Jquery and get it to prompt the user to save on it's return? @Html.ActionLink("download spreadsheet"​  How can I call an MVC FileContentResult by Jquery and get it to prompt the user to save on it's return? I'm generating a CSV from an MVC 3 website and using a FileContentResult to pass this to the user.

If you don't want to use an anchor, use a hidden iframe and set the src of the iframe to the url of the file to be downloaded.

<iframe id="hiddenFrame" src="" style="display:none; visibility:hidden;"></iframe>

Instead of the '$.post(...)' line use:

 downloadSpreadsheet('123');

 function downloadSpreadsheet(id) {
    var url = '@Url.Content("~/YourControllerName/GetSpreadsheet")' + "?id=" + id;
    $('#hiddenFrame').attr('src', url);
}

Or you can give a try on the jQuery Plugin for Requesting Ajax-like File Downloads

Using the plugin, you can download by invocking:

jQuery.download(url, data, method)

ASP.NET MVC Action Results and PDF Content, How can I call an MVC FileContentResult by Jquery and get it to prompt the user to save on it's return? - jquery. Kibria. Senior Software Engineer at Jaxara IT LTD. this user prefers to keep an air of mystery about them. 11 How can I call an MVC FileContentResult by

You can use this in JavaScript:

function downloadSpreadsheet(id) {
    window.location.href = '@Url.Action("GetSpreadsheet", "Home")?id=' + id;
}

Downloading file using ajax and jquery after submitting form data , I'm generating a CSV from an MVC 3 website and using a FileContentResult to I call an MVC FileContentResult by Jquery and get it to prompt the user to save  Browse other questions tagged asp.net-mvc pdf pdf-generation or ask your own question. Blog Podcast: Searching For The Next Frontier with Chris Dixon

Upload and download files using ASP.NET MVC – Rachel Appel, NET MVC provides a simple and versatile means of returning can be used to save the file locally on the client, and the browser would use to Figure 1 shows the result of the call as it shows up in FireBug. use FileStreamResult and you opt for FileContentResult if you have it available as a byte array. Is there a way to stream a file using ASP.NET MVC FileContentResult within the browser with a specific name? I have noticed that you can either have a FileDialog (Open/Save) or you can stream the file in a browser window, but then it will use the ActionName when you try to save the file.

Jquery read file from path, Calling a success Callback Jquery function after File Upload in MVC FileContentResult : Use this when you have obtained the file as a byte  The controller contains a method public ActionResult SaveFile () which returns a FileContentResult. The view contains a form, which submits to this action. The view contains some javascript to do an AJAX call to the same controller action where the form would post. Rather than triggering the aforementioned dialog,

ASP.Net MVC - free code corner, This is the same HTML and MVC code that you already know. sites it is unlikely that internet users would have privileges to save files there, FileContentResult : Use this when want to use a byte array to access the file. In this scenario, you might have obtained the file as a byte array from a database call. Thus the download can be made to happen by just setting the window.location to the url of the MVC Download action. But there are cases where we need to send a large collection or model to the MVC action which can only be done using HTTP POST, and thus we can no longer use the window.location solution to our rescue.

Comments
  • You can also set the content-type header to the appropriate type if known, to prompt an open or save dialog (in most cases) For example: Content-type: application/vnd.ms-excel or text/csv
  • Love the answer. Made me think about that song "Screamager" by Therapy.
  • What are those "absolutely obvious" reasons? I managed to get the file bytes using ajax, I just need to render them