简体   繁体   中英

Reporting services: Get the PDF of a generated report

I've a reporting services server which has already some running reports, and I need now to generate them through a custom website(running asp.net MVC3).

I need to retrieve this report as stream/byte to send it to the user. No "report viewer" or so.

Last time I used reporting services was with sql 2005, and We should ad as reference an obscure asmx file.

What's about now, with sql server reporting 2008 R2, .Net4 and visual studio 2010? I can't find a tutorial explaining the whole thing.

(in fact I can't find a tutorial where there is no report viewer for sql 2008 r2)

Several methods are provided at MSDN . I have used URL access often for quick simple PDF buttons in an ASP .NET app.

Here's a quick hack bit of code doing this. It could be cleaned up to use integrated authentication, and there are many ways you could store the report name. (I cut and pasted this from some old code I had that would store a report to a database and then later could return that from the db.

// 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 is

public static byte[] ReadFully( Stream input )
{
   using ( MemoryStream ms = new MemoryStream() )
   {
      input.CopyTo( ms );
      return ms.ToArray();
   }
}  

Because you have nested streams, you may want to close the first stream after the copy to avoid a closed stream error. Also, if you get a 401 Unauthorized, try default credentials.

            req.UseDefaultCredentials = true;
            req.PreAuthenticate = true;
            req.Credentials = CredentialCache.DefaultCredentials;

            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.
            HttpWResp.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM