Solved

Open BinaryReult(PDF File) in jQuery

Posted on 2010-11-11
2
2,312 Views
Last Modified: 2012-05-10
Hi,

I have a button "Open PDF" on page, I am calling controller through Jquery,My contorller returns JSon result with BinaryResult. In this binary result i have a PDF file.

Could you please say me how to open this pdf file in JQuery.

.aspx  Page:-
-----------------
<input type='button' class='compareButton' value='Open PDF' />

<script type="text/javascript">
 function OpenPDF(valuationID1, valuationID2) {
        var valuationId = valuationID1 + "," + valuationID2;        
        var url = '<%= Url.Action("Compare", "Dashboard") %>';
        $.post(url, { id: valuationId }, function (data) {
           
//*********************************Here i want to open that pdf file.**************************

        });
    }
</script>


.cs  Page:-
--------------
//This is my controller

 public class DashboardController : RctController
 {
   public JsonResult Compare(string id)
        {
            BinaryResult result = new BinaryResult();
            if (RctContext.Current.ClientData.GetParameter(Configuration.Model.Parameters.ShowComparisonReport_FLAG) == "Y")
            {
                string[] valuationId = Regex.Split(id, ",");
                ReportProvider provider = new ReportProvider();
                result = provider.GenerateComparisonReport(Convert.ToInt32(valuationId[0]));
            }
            return Json(result);
        }
}

public class ReportProvider
{
    public BinaryResult GenerateComparisonReport(int valuationId)
        {
            var context = NewContext().View(valuationId);

            using (var report = new ComparisonReport(context.Valuation))
            using (var exporter = new PdfExport())
            using (var reportStream = new MemoryStream())
            {
                report.Run();
                exporter.Export(report.Document, reportStream);
                return new BinaryResult
                {
                    ContentType = "application/pdf",
                    FileName = "Comparison_" + valuationId + ".pdf",
                    IsAttachment = true,
                    Data = reportStream.ToArray()                
                };
            }

        }
}

public class BinaryResult : ActionResult
{

        public byte[] Data { get; set; }
        public bool IsAttachment { get; set; }
        public string FileName { get; set; }
        public string ContentType { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.ContentType = ContentType;
            if (!string.IsNullOrEmpty(FileName))
            {
                context.HttpContext.Response.AddHeader("content-disposition", ((IsAttachment) ? "attachment;filename=" : "inline;filename=") + FileName);
            }
            context.HttpContext.Response.BinaryWrite(Data);
        }
}


I attached output of Binary Result object.
That how my Binary Result look like.

Could you please help me to open this pdf file in jquery.

Thanks in Advance













ObjectInBinaryResult.docx
0
Comment
Question by:kravindra
2 Comments
 
LVL 7

Accepted Solution

by:
dxdinh earned 500 total points
ID: 34113396
1.  I would setup and iframe and using jQuery to update the URL link to stream down the pdf. since you need to update header content for the browser to use pdf reader to open it.

   content="application/pdf"
2. setup the ViewPDF.aspx to return the BinaryResult as you would with appropriate header and such

 function OpenPDF
{
   var url Post = 'build your URL here';

   $("#frmViewPDF").attr('action', urlPost  );
   
   $("#frmViewPDF").submit();

}

 <form  id="frmViewPDF" action="ViewPDF.aspx?yourParamHere" method="post" target="_blank" >

</form>
0
 

Author Closing Comment

by:kravindra
ID: 34114317
thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now