c# - Asp .Net Mvc:如何通过 Ajax 调用加载 RDLC 报告
问题描述
我有一个控制器 Action,它通过 return File() 函数返回 RDLC 报告。通过浏览器地址栏调用该Action时输出正常。同样,当通过 ajax GET 方法调用此操作时,成功数据是下面的字符串。我只想在新窗口中将 ajax 调用的输出显示为 PDF 文件。有办法吗?我不想将参数作为 window.open() 函数的查询字符串发送,以便从浏览器地址栏中调用 Action。
success: function (data) {
var w = window.open();
$(w.document.body).html(data);
}
%PDF-1.3 1 0 obj [/PDF /Text /ImageB /ImageC /ImageI] endobj 4 0 obj
<< /Length 123 /Filter /FlateDecode>> stream X }N� �0���M���\�籅v�8�� ��u> /Font
<< /F3 3 0 R>> >> >> endobj 3 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding>> endobj 5 0 obj
<< /Type /Pages /Kids [ 2 0 R ] /Count 1>> endobj 6 0 obj
<< /Type /Catalog /Pages 5 0 R>> endobj 7 0 obj
<< /Title /Author <> /Subject
<> /Creator (Microsoft Reporting Services 10.0.0.0) /Producer (Microsoft Reporting Services PDF Rendering Extension 10.0.0.0) /CreationDate (D:20180827103611+06'00') >> endobj xref 0 8 0000000000 65535 f 0000000010 00000 n 0000000266 00000 n
0000000429 00000 n 0000000065 00000 n 0000000529 00000 n 0000000591 00000 n 0000000643 00000 n trailer
<< /Size 8 /Root 6 0 R /Info 7 0 R>> startxref 957 %%EOF
解决方案
你必须代替 window.open() 使用这个
success: function (data) {
var w = window.open();
w.location = '/Cashier/CashDesk/Download';
/*go to address get methon*/
},
您可以轻松地将代码放入此操作中
public ActionResult Download()
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Reports/PatientRecipt.rdlc");
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "DataSet1";
//reportDataSource.Name = "PatientRecipt";
reportDataSource.Value = applicationDbContext.TblInvoices.Select(x => x).ToList();
localReport.DataSources.Add(reportDataSource);
string reporttype = "PDF";
string mimeType;
string encoding; string[] streams;
Warning[] warnings;
byte[] renderedByte;
renderedByte = localReport.Render(reporttype, "", out mimeType, out encoding, out reporttype, out streams, out warnings);
Response.AddHeader("content-disposition", "attachment;filename = expens_report." + reporttype);
return File(renderedByte, reporttype);
}
推荐阅读
- python - 使用变量作为格式精度的麻烦
- php - PHP 7.1.22 CLI 已停止工作 windows server 2008 R2
- codenameone - 代号 一个应用程序在宽屏幕上以居中的矩形显示
- vb.net - 如何在 GeckoFX 60 中处理下载
- javascript - 在 Angular 中比较两个字符串的问题 - 6
- git - 我想撤消提交并保留更改
- google-apps-script - 电子邮件通知上的单元格引用超出范围
- amazon-web-services - 可以从直接调用和 SNS 消息中获取参数的 Lambda
- javascript - 如何用 chrome 扩展中的其他页面替换弹出窗口?
- javascript - 带有 CSS 转换问题的页面转换