我已经看到了许多使用 Web API 从存储在磁盘上的文件返回 PDF(或其他文件类型)的示例。但是,就我而言,我正在尝试使用 SSRS 的 URL Access 动态生成文档。这是 URL 的示例:
https://reporting.mydomain.biz/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=%2fquest%2fQUEST%2520Reports%2fReview.rdl&rp%3aReview=220
我尝试了多种方法,但其中大多数都可以追溯到 2012 年,并且与以下相关ASP.Net MVC
:
public async Task<FileStreamResult> GenerateReport()
{
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://domainORipaddress"), "NTLM", new NetworkCredential(
ConfigurationManager.AppSettings["username"],
ConfigurationManager.AppSettings["password"]
));
Stream report = null;
using (var httpClient = new HttpClient(new HttpClientHandler { Credentials = credentialCache }))
{
httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
report = await httpClient.GetStreamAsync("reportUrl");
}
var contentDisposition = new ContentDisposition
{
FileName = "Report.pdf",
Inline = false
};
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
return File(report, "application/pdf");
//or use
//Response.AppendHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", reportName));
//return File(reportPath, MediaTypeNames.Application.Pdf);
}
此示例代码来自此网站:http ://webstackoflove.com/sql-server-reporting-service-with-asp-net-mvc/
这是一个类似的问题,但基于ASP.NET
:报告服务:获取生成报告的 PDF
我尝试使用上面的代码返回 aHttpResponseMessage
但我得到一个没有数据的文件。
这是我现在使用的,它返回一个 0 KB 的文件:
public HttpResponseMessage PrintQualityReview([FromUri] int reviewId)
{
var reportUrl = String.Format("https://reporting.mydomain.biz/quest/_vti_bin/reportserver?https://reporting.mydomain.biz/quest/QUEST%20Reports/{0}Review.rdl&rs:Format=PDF&Review={1}",
ConfigurationManager.AppSettings["ReportingServiceDatabase"],
reviewId);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("https://reporting.mydomain.biz"), "NTLM", new NetworkCredential(
ConfigurationManager.AppSettings["ReportingServiceAccount"],
ConfigurationManager.AppSettings["ReportingServiceAccountPwd"]
));
MemoryStream mStream = new MemoryStream();
using (WebClient wc = new WebClient())
{
wc.Credentials = credentialCache;
using (Stream stream = wc.OpenRead(reportUrl))
{
stream.CopyTo(mStream);
}
}
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(mStream);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition.FileName = "Report.pdf";
return response;
}
我想知道如何处理他的ASP.Net Web API
?