Downloading files using ASP.NET .ashx modules

w1z picture w1z · Feb 23, 2010 · Viewed 18.7k times · Source

I have ASP.NET page with an iframe on it for displaying some pdf reports on this page. When user select the report type from dropdown, I add the needed for report data into the ASP.NET Session and change the attribute "src" of the iframe to .ashx module address which generates the .pdf report. But if Adobe glug-in for viewing .pdf files in browser is not installed, browser proposes to save report file and name of the proposed file is "HandlerName.ashx". But I want to make the browser proposed to save the file with the name "Report.pdf". Can I do this? Are there any workarounds?

Answer

Pbirkoff picture Pbirkoff · Feb 23, 2010

Add the following header in your ashx-file:

context.Response.AddHeader("content-disposition", "attachment;filename=PUTYOURFILENAMEHERE.pdf");
context.Response.ContentType = "application/pdf";