?
Solved

Open BinaryReult(PDF File) in jQuery

Posted on 2010-11-11
2
Medium Priority
?
2,422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
dxdinh earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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