HttpHandler and "Save As" prompt
These days I work a lot with Reporting Services and an ASP.NET application which serve reports on an aspx page.
The first implementation was to put a button on the page and call the Reporting Services web service to render the report.
But now the customer asked to change the file name of the
generated pdf reports, more than this he wants the file name
to change dynamically depending on parameters.
By
default the exported report file name is the name of the
report (the rdl file), and I don't see anything on the
Reporting Web Service to change this.
But RS web service also expose one method to render the
report as an array of bytes. So I created a HttpHandler to
fetch the report's bytes and flush it in the response
content. The HttpHandler allows me to control the generated
file name.
The problem is pdf is a known file type so
the browser open Adobe Acrobat instead of asking the user to
save the file. I knew this is possible so I was happy to
google and find this entry from Andrew L. Van Slaars :
Forcing an "open, save" prompt for known file types
Hope this will bring more exposure to the tip.
I end up with this piece of code for my HttpHandler :
public class ReportHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { string reportName = context.Request["reportName"]; string fileName = string.Empty; byte[] buffer = null; // Method from the business layer // Get the report as an array of bytes // Build the file name buffer = GetQuoteReportAsPdfUrl(reportName, out fileName); context.Response.Clear(); context.Response.ContentType = "application/pdf"; // This line opens the Save prompt instead of Adobe Acrobat context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); context.Response.AddHeader("Content-Length", buffer.Length.ToString()); context.Response.OutputStream.Write(buffer,0,buffer.Length); context.Response.End(); } public bool IsReusable { get { return true; } } }