M
mo
I have an application that uses Reporting Services. When the user
chooses to print a report, they are taken to a window that allows them
to fill in parameters for the report. They then click a button to
either export to PDF or to EXCEL. Once the report is generated, the
byte array containing the data is put into session and an aspx page is
loaded into a hidded iframe that "prints" the report (using the byte
array from session) causing the file download dialog to appear. If you
choose "save", everything is fine - the report saves correctly. If you
choose "open", the file "cannot be found" either in Excel or
Acrobat/PDF.
If I skip the iframe, using the same page to print the report that is
used to select the params, everything works fine. I am baffled...!
Code to follow:
/* BEGIN Report Parameter Selector Page */
private void ibtnPDF_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
/* CODE LEFT OUT FOR SIMPLICITY */
rb = new SQLReportBuilder( parameterCount );
rb.ReportName = reportName;
rb.ReportPath = reportDirectory;
rb.SetRenderingFormat( SQLReportBuilder.ReportFormat.PDF );
LoadReport();
}
private void LoadReport()
{
string encoding = null;
string mimeType = null;
string[] streamIDs = null;
Warning[] warnings = null;
ParameterValue[] usedParams = null;
/* CODE LEFT OUT FOR SIMPLICITY */
byte[] result = rs.Render( rb.ReportPath + rb.ReportName,
rb.Format, rb.HistoryID, rb.DeviceInfo, rb.Parameters,
rb.DataSourceCredentials, rb.ShowHideToggle, out encoding, out
mimeType, out usedParams, out warnings, out streamIDs );
Session["__REPORTDATA"] = result;
Session["__OUTPUTFILENAME"] = reportName + rb.ReportFileExtension;
this.iframeReportDownload.Attributes.Add( "src",
"ReportDownloader.aspx" );
}
/* END Report Parameter Selector Page */
/* BEGIN Report Printing Page (ReportDownloader.aspx)*/
byte[] result = null;
string outputFileName = null;
private void Page_Load(object sender, System.EventArgs e)
{
result = (byte[])Session["__REPORTDATA"];
outputFileName = (string)Session[ "__OUTPUTFILENAME" ];
if ( result != null )
{
PrintReport();
}
}
private void PrintReport()
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader( "Content-Disposition", "attachment; filename="
+ outputFileName );
Response.AddHeader( "Content-Length", result.Length.ToString() );
Response.BinaryWrite( result );
Response.Flush();
Response.End();
}
/* END Report Printing Page */
If I put the exact code that is in the PrintReport() function into the
LoadReport() function, in place of the line
"this.iframeReportDownload.Attributes.Add( "src",
"ReportDownloader.aspx" );" - it works just fine - I can save and open.
Does anyone know why I cannot open the file but I can save the file???
chooses to print a report, they are taken to a window that allows them
to fill in parameters for the report. They then click a button to
either export to PDF or to EXCEL. Once the report is generated, the
byte array containing the data is put into session and an aspx page is
loaded into a hidded iframe that "prints" the report (using the byte
array from session) causing the file download dialog to appear. If you
choose "save", everything is fine - the report saves correctly. If you
choose "open", the file "cannot be found" either in Excel or
Acrobat/PDF.
If I skip the iframe, using the same page to print the report that is
used to select the params, everything works fine. I am baffled...!
Code to follow:
/* BEGIN Report Parameter Selector Page */
private void ibtnPDF_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
/* CODE LEFT OUT FOR SIMPLICITY */
rb = new SQLReportBuilder( parameterCount );
rb.ReportName = reportName;
rb.ReportPath = reportDirectory;
rb.SetRenderingFormat( SQLReportBuilder.ReportFormat.PDF );
LoadReport();
}
private void LoadReport()
{
string encoding = null;
string mimeType = null;
string[] streamIDs = null;
Warning[] warnings = null;
ParameterValue[] usedParams = null;
/* CODE LEFT OUT FOR SIMPLICITY */
byte[] result = rs.Render( rb.ReportPath + rb.ReportName,
rb.Format, rb.HistoryID, rb.DeviceInfo, rb.Parameters,
rb.DataSourceCredentials, rb.ShowHideToggle, out encoding, out
mimeType, out usedParams, out warnings, out streamIDs );
Session["__REPORTDATA"] = result;
Session["__OUTPUTFILENAME"] = reportName + rb.ReportFileExtension;
this.iframeReportDownload.Attributes.Add( "src",
"ReportDownloader.aspx" );
}
/* END Report Parameter Selector Page */
/* BEGIN Report Printing Page (ReportDownloader.aspx)*/
byte[] result = null;
string outputFileName = null;
private void Page_Load(object sender, System.EventArgs e)
{
result = (byte[])Session["__REPORTDATA"];
outputFileName = (string)Session[ "__OUTPUTFILENAME" ];
if ( result != null )
{
PrintReport();
}
}
private void PrintReport()
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader( "Content-Disposition", "attachment; filename="
+ outputFileName );
Response.AddHeader( "Content-Length", result.Length.ToString() );
Response.BinaryWrite( result );
Response.Flush();
Response.End();
}
/* END Report Printing Page */
If I put the exact code that is in the PrintReport() function into the
LoadReport() function, in place of the line
"this.iframeReportDownload.Attributes.Add( "src",
"ReportDownloader.aspx" );" - it works just fine - I can save and open.
Does anyone know why I cannot open the file but I can save the file???