[英]ASP.Net MVC Access the SSRS reports and export them to pdf without using ReportViewer
So, I am using SSRS to generate reports and I have a server where my reports are stored.因此,我正在使用 SSRS 生成报告,并且我有一个存储报告的服务器。 Now I need to download them and I have this code in my controller:
现在我需要下载它们,我的 controller 中有这段代码:
public ActionResult Export(string id)
{
NetworkCredential nwc = new NetworkCredential("username", "password");
WebClient client = new WebClient();
client.Credentials = nwc;
client.UseDefaultCredentials = true;
client.Credentials = CredentialCache.DefaultCredentials;
string reportURL = "http://desktop-4gpisbo:8080/teste/report/RelatorioPrincipal/&rs:Command=Render&rs:Format=PDF&rc:Toolbar=false&rc:Parameters=false&id=" + id;
return File(client.DownloadData(reportURL), "application/pdf");
}
But it is giving me a PDF that is impossible to open or when it opens it says that is unavailable.但它给了我一个无法打开的 PDF,或者当它打开时它说不可用。 The
id
is being passed via JQuery
in my view, where it gets the Id
from the URL
then sends it to the controller, because my report is dynamic, I need different information depending on the user.在我看来,
id
是通过JQuery
传递的,它从URL
获取Id
,然后将其发送到 controller,因为我的报告是动态的,我需要不同的信息,具体取决于用户。
What is wrong with this code?这段代码有什么问题?
PS: The report is running on my localhost not on a server PS:报告在我的本地主机上运行,而不是在服务器上运行
private void SetDataSources(object sender, SubreportProcessingEventArgs e)
{
e.DataSources.Add(new
ReportDataSource("DataSources1",TempData["DataSources1"]));
}
public FileResult ExportToPDF(TableViewModel data, string setToken)
{
var ProjectsList = from project in ProjectList
select new
{
project.ID,
project.Name,
};
ReportViewer ProjectListReportViewer = new ReportViewer();
ProjectListReportViewer.LocalReport.ReportPath =
Server.MapPath("~/Views/Reports/Reprot.rdlc");
ProjectListReportViewer.LocalReport.DataSources.Clear();
ReportDataSource rdc = new ReportDataSource("DataSet1", ProjectsList);
ProjectListReportViewer.LocalReport.SubreportProcessing += new
SubreportProcessingEventHandler(SetProjectsDetail);
ProjectListReportViewer.LocalReport.DataSources.Add(rdc);
ProjectListReportViewer.LocalReport.EnableExternalImages = true;
ProjectListReportViewer.LocalReport.Refresh();
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = ProjectListReportViewer.LocalReport.Render("PDF", null,
out mimeType, out encoding, out extension, out streamIds, ouwarnings);
ControllerContext.HttpContext.Response.Cookies.Add(new
HttpCookie("getToken", setToken));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "Report.pdf");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.