Tuesday, October 28, 2008

How to Enforce Browser to Download Files without Opening them

By default browsers seek the suitable program to open any file you are downloading and in some applications it is required to download the file and save it without opening it, so in the this article I am showing you how to enforce browser to download files without opening them via .NET code.
  1. Create a new ASP.NET web page.
  2. Add a button on it.
  3. In the server side click event add the following VB.NET code which create text file and send it to the browser as a binary response.
Dim FileContents As String = "I am a Simple Text File"
Response.AddHeader("content-disposition", "attachment;filename=Untitled.txt")

Response.BinaryWrite(Encoding.ASCII.GetBytes(FileContents))
Response.End()

How to Make your ASP.NET 2.0 Application Unavailable

Create HTML file and name it app_offline.htm then place this file into the root of the application.

When ASP.NET finds it, it will shut-down the app-domain and will stop all requests. and will send back the contents of the app_offline.htm file in response to all new dynamic requests for the application. and to enable the site again just delete or rename the file and the site will come back online automatically.