How to allow download of file, that is returned as binary data from AJAX

My problem is that I send to client first PDF to download, then I need to check, if some data exists in my database, then depending on that check I need to show question that if user want to download another PDF, that I generates.

My Code:

 //Here I just make dialog for question
 $('#printDWInfo').dialog({
            resizable: false,
            modal: true,
            autoOpen: false
        });

 //Here is my problem :)
 $('#generujWydruk').click(function (event) {
            event.preventDefault();
            $('#printForm').submit(); // <-- sending first request and client get first PFD file
            $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>', <-- check for another data
                $("#printForm").serialize(),
                function(data) {
                    if (data.length > 0) {
                        $("#printDWInfo").dialog( "option", "buttons", [
                            {
                                text: "Tak",
                                click: function () {
                                    $.ajax({ type: "POST",
                                        url: '<%= Url.Action("PrintDWList","Reports")%>',
                                        datatype: "json",
                                        traditional: true,
                                        data:{'ids': data },
                                        success: function (data2) {
                                            //I don't know what to do here
                                        }
                                    });
                                    $(this).dialog("close");
                                    }
                                }, {
                                text: "Nie",
                                click: function () {
                                    $(this).dialog("close");
                                    }
                            }
                        ]);
                        $('#printDWInfo').dialog("open");
                    }
                }
            );

If client click on button "Tak" in dialog, I use ajax request, because I can pass to controler array of int, that is returned by $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>'. In success function of my ajax request FireBug show me that data2 is binary data of my PDF file, what I need to do to allow client to download this PDF file?

Answers:

Answer

In success function of my ajax request FireBug show me that data2 is binary data of my PDF file, what I need to do to allow client to download this PDF file?

You should not use AJAX to download files. The problem is that you are fetching the pdf bytes in a javascript variable in the success callback of your AJAX call but there's nothing you could do with it. You cannot prompt the user to save it and of course you cannot save it to the client since javascript doesn't have the necessary privileges.

So you should use a normal request:

var downloadUrl = '<%= Url.Action("PrintDWList", "Reports")%>?' + $.param({ ids: data }, true);
window.location.href = downloadUrl;

Notice that this will send a GET request to the PrintDWList action passing the ids query string parameter so make sure this action is accessible on GET. Now if the controller action uses the Content-Disposition header to attachment it will offer the user to download the file:

public ActionResult PrintDWList(int[] ids)
{
    byte[] pdf = ...
    return File(pdf, "application/pdf", "file.pdf");
}
Answer

@Darin is a great help always on this forum. His response saved my day too. Just extending his answer to include the formats and stuff that I did. This is in response to his " Now if the controller action uses the Content-Disposition header to attachment it will offer the user to download the file:"

                case "PDF": 
                    HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".pdf");
                    break;

                case "XML": HttpContext.Current.Response.AddHeader("Content-Type", "application/xml");
                    break;

                case "MHTML": HttpContext.Current.Response.AddHeader("Content-Type", "message/rfc822");
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".mhtml");
                    break;

                case "EXCEL": HttpContext.Current.Response.AddHeader("Content-Type", "application/vnd.ms-excel");
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".xls");
                    break;

                case "WORD": HttpContext.Current.Response.AddHeader("Content-Type", "application/msword");
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".doc");
                    break;

                case "CSV":
                    HttpContext.Current.Response.AddHeader("Content-Type", "text/csv");
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".csv");
                    break;

                case "HTML4.0": HttpContext.Current.Response.AddHeader("Content-Type", "message/rfc822"); ;
                    break;

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.