Reputation: 21661
Here's the code I'm using to display report from a SSRS server in PDF format.
var reportServerUrl = new Uri(string.Format("{0}?{1}{2}&rs:aCommand=Render&rs:Format=pdf",
ConfigurationManager.AppSettings("ReportServerURL"),
ConfigurationManager.AppSettings("ReportFolder"), ItemPath));
var wcli = new WebClient();
wcli.Credentials = CustomReportCredentials.WebClientCredentials;
var fileBuffer = wcli.DownloadData(reportServerUrl);
if (fileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", fileBuffer.Length.ToString());
Response.BinaryWrite(fileBuffer);
}
It's works most of the time. However, sometimes when I click a link, instead of getting a report in PDF format, I'm getting some strange results, depending of the browser. In CHROME, I'm getting the source code of the page. In IE, I'm getting the raw response + some stange characters (see image). Then the page times out.
Honestly, I don't have a clue why this is happening.
Thanks for helping.
Upvotes: 1
Views: 719
Reputation: 6606
What you see there is a raw binary HTTP response including headers and payload (that is what's making up those weird chracters) interpreted as HTML. I think what you are missing here is a call to Response.Clear()
after setting your headers up:
if (fileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", fileBuffer.Length.ToString());
Response.Clear();
Response.BinaryWrite(fileBuffer);
}
Failing to do so will leave the webserver's default headers in place.
Having said that, I think you do not really need to set the content length of your payload. See the example for Response.Flush()
.
Upvotes: 1