Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Open BinaryReult(PDF File) in jQuery

Posted on 2010-11-11
2
Medium Priority
?
2,457 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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