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) {
//************************ *********H ere 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.Client Data.GetPa rameter(Co nfiguratio n.Model.Pa rameters.S howCompari sonReport_ FLAG) == "Y")
{
string[] valuationId = Regex.Split(id, ",");
ReportProvider provider = new ReportProvider();
result = provider.GenerateCompariso nReport(Co nvert.ToIn t32(valuat ionId[0])) ;
}
return Json(result);
}
}
public class ReportProvider
{
public BinaryResult GenerateComparisonReport(i nt valuationId)
{
var context = NewContext().View(valuatio nId);
using (var report = new ComparisonReport(context.V aluation))
using (var exporter = new PdfExport())
using (var reportStream = new MemoryStream())
{
report.Run();
exporter.Export(report.Doc ument, 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(ControllerCo ntext context)
{
context.HttpContext.Respon se.Clear() ;
context.HttpContext.Respon se.Content Type = ContentType;
if (!string.IsNullOrEmpty(Fil eName))
{
context.HttpContext.Respon se.AddHead er("conten t-disposit ion", ((IsAttachment) ? "attachment;filename=" : "inline;filename=") + FileName);
}
context.HttpContext.Respon se.BinaryW rite(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
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) {
//************************
});
}
</script>
.cs Page:-
--------------
//This is my controller
public class DashboardController : RctController
{
public JsonResult Compare(string id)
{
BinaryResult result = new BinaryResult();
if (RctContext.Current.Client
{
string[] valuationId = Regex.Split(id, ",");
ReportProvider provider = new ReportProvider();
result = provider.GenerateCompariso
}
return Json(result);
}
}
public class ReportProvider
{
public BinaryResult GenerateComparisonReport(i
{
var context = NewContext().View(valuatio
using (var report = new ComparisonReport(context.V
using (var exporter = new PdfExport())
using (var reportStream = new MemoryStream())
{
report.Run();
exporter.Export(report.Doc
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(ControllerCo
{
context.HttpContext.Respon
context.HttpContext.Respon
if (!string.IsNullOrEmpty(Fil
{
context.HttpContext.Respon
}
context.HttpContext.Respon
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER