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.
- Create a new ASP.NET web page.
- Add a button on it.
- 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()
Response.AddHeader("content-disposition", "attachment;filename=Untitled.txt")
Response.BinaryWrite(Encoding.ASCII.GetBytes(FileContents))
Response.End()