MSDN提供了几种方法。我经常使用URL 访问来获取 ASP .NET 应用程序中的快速简单 PDF 按钮。
这是执行此操作的快速破解代码。可以清理它以使用集成身份验证,并且可以通过多种方式存储报告名称。(我从一些旧代码中剪切并粘贴了它,这些代码会将报告存储到数据库中,然后可以从数据库中返回它。
// First read in the report into memory.
string strReportUser = "RSUserName";
string strReportUserPW = "MySecretPassword";
string strReportUserDomain = "DomainName";
string sTargetURL = "http://SqlServer/ReportServer?" +
"/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" +
ParamValue;
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create( sTargetURL );
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential(
strReportUser,
strReportUserPW,
strReportUserDomain );
HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();
Stream fStream = HttpWResp.GetResponseStream();
//Now turn around and send this as the response..
byte[] fileBytes = ReadFully( fStream );
// Could save to a database or file here as well.
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader(
"content-disposition",
"attachment; filename=\"Report For " +
ParamValue + ".pdf\"" );
Response.BinaryWrite( fileBytes );
Response.Flush();
HttpWResp.Close();
Response.End();
ReadFully 是
public static byte[] ReadFully( Stream input )
{
using ( MemoryStream ms = new MemoryStream() )
{
input.CopyTo( ms );
return ms.ToArray();
}
}