Search
Back to all

Display a PDF from Jscipt Ajax Call

I searched for the answer for a while before I found this works

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@Html.Raw(Url.Action("DailyReport"))?date=" + $('#EventStartDateTimeUtc').val() +
                        "&groupProductId=" + ddlGroupProductId + "&associatedProductId=" + ddlAssociatedProducts,
                    xhrFields: { responseType: "blob" },
                    success: function (data) {
                        var blob = new Blob([data]);
                        var link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = "Daily_Report_" + $('#EventStartDateTimeUtc').val() + ".pdf";
                        link.click();
                        var file = new File([blob], filename, { type: 'application/force-download' });
                        window.open(URL.createObjectURL(file));
                    },
                    error: function (error) {
                        alert("Error: " + error);
                    }
                });

Comments
Leave your comment Close