Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

c# - Asp.Net Core: Download SSRS reports in PDF format on client browser

I have one SSRS report URL which if I enter that URL in the browser then it shows the SSRS report with Print and Save option like:

enter image description here

Which works fine on the browser.

What I want, there is one button on .cshtml page so by clicking on that button I need to download the report on client browser.

URL: http://xyz/ReportS/report/UAT/SampleReport_V1?Srno=X123

What I tried:

by setting &rs:Format=PDF and made a HTTP Request from Asp.Net Core Application but it generated PDF but in corrupt format.

Code:

public void Download_SSRSInPDF()
{
    string URL = "http://xyz/ReportS/report/UAT/SampleReport_V1";
    string Command = "Render";
    string Format = "PDF";
    URL = URL + "?SrNo=X123&rs:Command=" + Command + "&rs:Format=" + Format;
    System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);

    Req.Credentials = System.Net.CredentialCache.DefaultCredentials;
    Req.Method = "GET";
    string path = @"E:New folderTest.pdf";
    System.Net.WebResponse objResponse = Req.GetResponse();
    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
    System.IO.Stream stream = objResponse.GetResponseStream();
    byte[] buf = new byte[1024];
    int len = stream.Read(buf, 0, 1024);

    while (len > 0)
    {
        fs.Write(buf, 0, len);
        len = stream.Read(buf, 0, 1024);
    }
    stream.Close();
    fs.Close();
}

How to do it in Asp.Net Core?

EDIT::

I have that asmx url of my report:

http://server-name/ReportServer/reportexecution2005.asmx

But unable to proceed with this, can someone points me towards documentation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're better off doing this using the supported ReportExecution2005.asmx endpoint with something like WCF, instead of trying to use HTTP web request to impersonate a user interaction in a browser.

Once you've got it setup, there are Render methods on the report execution instance that make it really easy to get the report as a PDF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...