Link to home
Start Free TrialLog in
Avatar of kravindra
kravindra

asked on

Open BinaryReult(PDF File) in jQuery

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
ASKER CERTIFIED SOLUTION
Avatar of dxdinh
dxdinh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kravindra
kravindra

ASKER

thanks