C# SSRS中直接url中损坏的PDF
我正在尝试生成一个指向我的报告的直接url,以便它可以作为PDF下载,而不是重定向到报告UI。 如果我在浏览器上粘贴url,它只会将我重定向到报告页面。 我找到了两种解决方案,但都生成了损坏的pdfC# SSRS中直接url中损坏的PDF,c#,reporting-services,C#,Reporting Services,我正在尝试生成一个指向我的报告的直接url,以便它可以作为PDF下载,而不是重定向到报告UI。 如果我在浏览器上粘贴url,它只会将我重定向到报告页面。 我找到了两种解决方案,但都生成了损坏的pdf public ActionResult GetReportingData() { var theURL = "http://servername/Reports_DomainName/Pages/Report.aspx?ItemPath=%2f
public ActionResult GetReportingData()
{
var theURL = "http://servername/Reports_DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2_TST%2fReportTest&year=2019&rs:Format=PDF";
WebClient Client = new WebClient();
Client.Credentials = new NetworkCredential(@"username", "password");
byte[] myDataBuffer = Client.DownloadData(theURL);
var filename = "Test.pdf";
var path = "C:\\Test";
var fileLocation = path + "\\" + filename;
System.IO.File.WriteAllBytes(fileLocation, myDataBuffer);
return RedirectToAction("Index", "Case");
}
我更熟悉从数据(通过SSRS LocalReport)生成PDF,这使我能够完全控制。但是,如果您这样调整第一个选项,会发生什么情况:
public async Task<ActionResult> GetReportingData()
{
var theURL = "http://servername/Reports_DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2_TST%2fReportTest&year=2019&rs:Format=PDF";
try
{
WebClient Client = new WebClient();
Client.Credentials = new NetworkCredential(@"username", "password");
byte[] data = Client.DownloadData(theURL);
return new FileContentResult(data, "PDF") { FileDownloadName = String.Format("SomeReport_{0}.pdf", DateTime.Now.ToString("yyyyMMddhhmmss")) };
}
catch(Exception ex)
{
// do something
}
}
公共异步任务GetReportingData()
{
var theURL=”http://servername/Reports_DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2_TST%2fReportTest&year=2019&rs:Format=PDF";
尝试
{
WebClient客户端=新的WebClient();
Client.Credentials=新的网络凭据(@“用户名”、“密码”);
字节[]数据=客户端。下载数据(URL);
返回新的FileContentResult(data,“PDF”){FileDownloadName=String.Format(“SomeReport_{0}.PDF”,DateTime.Now.ToString(“yyyyMMddhhmmss”))};
}
捕获(例外情况除外)
{
//做点什么
}
}
这将提示您从浏览器保存文件。然后尝试打开该文件,看看它是否仍然损坏。您为生成PDF而调用的URL可能在响应内容中返回其他杂音,因此不只是报告/PDF的字节数组。我发现了错误
这是asnwer的链接:
基本上,我使用的是web门户url(错误),而不是web服务url(正确):
原始(错误url):报告\u DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2\u TST%2freeporttest&year=2019&rs:Format=PDF
正确的url:ReportServer\u DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2\u TST%2freeporttest&year=2019&rs:Format=PDF
public async Task<ActionResult> GetReportingData()
{
var theURL = "http://servername/Reports_DomainName/Pages/Report.aspx?ItemPath=%2fMQMS2_TST%2fReportTest&year=2019&rs:Format=PDF";
try
{
WebClient Client = new WebClient();
Client.Credentials = new NetworkCredential(@"username", "password");
byte[] data = Client.DownloadData(theURL);
return new FileContentResult(data, "PDF") { FileDownloadName = String.Format("SomeReport_{0}.pdf", DateTime.Now.ToString("yyyyMMddhhmmss")) };
}
catch(Exception ex)
{
// do something
}
}