Sending files to the browser

Sending files to the browser

The ASP.NET Response object has been greatly expanded in its capability to send output to the client browser. For example, the WriteFile method can send the contents of any text, HTML, or XML file to the browser; in classic ASP you have to load the file in memory and then pass its contents to a Write method to reach the same effect. For example, you can use this method to apply a common frame and style, typically a menu bar or footer, to any .txt file:

' Get the name of the requested document (passed on the query string).Dim path As String = Request.QueryString("doctitle") & ".txt"' Convert to a physical path.path = Request.MapPath(path)If System.IO.File.Exists(path) Then    ' If the file exists, send it to the browser as HTML.    Response.Write("")    Response.Write("

Here's the document you've requested

") Response.WriteFile(path) Response.Write("")Else ' Else display an error message. Response.Write("Sorry, no document with this name.")End If


Share the Post: