0

I have a ajax request to apiurl where i am getting an PDF File. Now i want to display this file in a new window

$("#pdfurl").click(function (e) {
    var Pdfurl = $(this).attr('data-href');
    $.ajax({
        url: "../RequestPages/PreviewPdf",
        type: "GET",
        data: { "pdfUrl": Pdfurl },
        success: function (data) { 
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
})



 public ActionResult PreviewPdf(string pdfUrl)
{
    var web = new WebClient();
    byte[] bytes = web.DownloadData(pdfUrl);
    string mimeType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "inline; filename=" + "a.pdf");
    return File(bytes, mimeType);
}`
mplungjan
  • 169,008
  • 28
  • 173
  • 236
user202
  • 758
  • 10
  • 26

2 Answers2

5

The point of an AJAX request is to give you the response as Javascript data.

If you want the browser to navigate to the response, you can't use AJAX.

Instead, just use open() to open the URL directly.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

The pragmatic in me suggests

<a href="../RequestPages/PreviewPdf?pdfUrl=bla.pdf" target="_blank">PDF</a>

Never use script if it is not necessary

mplungjan
  • 169,008
  • 28
  • 173
  • 236