none
Display PDF stream returned from controller in new window RRS feed

  • Question

  • I am working on a project and I need to display a pdf in a new tab, I have a controller which gets the pdf from a reporting service as a byte[].

        var data = Search(search_info);
        var stream = new MemoryStream(data, 0, data.Length, true, true);    
        Response.Clear();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "filename=\"\"" + "caseoverview" + ".pdf" + "");
        Response.ContentType = MimeTypes.ApplicationPdf;
        Response.OutputStream.Write(data1, 0, Convert.ToInt32(stream.Length));
        Response.Flush();
        return new FileStreamResult(stream, "application/pdf");

    I am using Ajax to call the controller:

      @using (Ajax.BeginForm("Search", "Controller", null,
                    new AjaxOptions
                    {
                        HttpMethod = "post",
                        OnComplete = "OnCompleteMethod",
                        OnFailure = "OnFailtureMethod"
                    }))
                {
       
                    <div id="query-filter" class="filters">
                        @Html.Partial("QueryFilter", Model)
                    </div>
                    <div class="row">
                        <div class="col-md-2 col-sm-6 col-md-offset-8">
                            <button class="btn btn-submit btn-block" type="submit">Find</button>
                        </div>
                        <div class="col-md-2 col-sm-6">
                            <button class="btn btn-submit btn-block" type="reset">Reset</button>
                        </div>
                    </div>
                }

    And the javascript for the success method is:

     function OnCompleteMethod(dataq, status) {
            if (status === "success") {
    
                var w = window.open("data:application/pdf, " + escape(dataq.responseText));
                w.document.write(dataq.responseText);
                w.document.close();
        }
        }
    

    And this is what I am getting in the new tab:



    Can someone please help me with this problem, and explain to me what I am doing wrong or what I am missing from the solution.

    Thank you so much.

    • Moved by Limitxiao Gao Wednesday, August 30, 2017 9:31 AM Not related to TFS
    Monday, August 28, 2017 1:34 PM

All replies

  • Hi yfrancisco,

    Our forum is for TFS on premise.

    As your problem is how to show PDF on webpage, it is related to ASP.NET. Please post a new thread in this forum for professional assistance.

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 30, 2017 6:28 AM