Solved

Open BinaryReult(PDF File) in jQuery

Posted on 2010-11-11
2
2,281 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

13 Experts available now in Live!

Get 1:1 Help Now