Saturday, 10 March 2007

Dynamic Text Files

Ever wanted to dynamically create downloadable text files on your website? It couldn't be any simpler using a web scripting language, such as ASP or PHP. All that is required is to modify the response header of a dynamic web page. The text file does not actually exist on the web server. Instead, it is created and sent directly to the web client.

The following ASP code sample demonstrates how this can be achieved:

Response.ContentType = "application/text"
Response.AddHeader "Content-Disposition", _
"attachment; filename=greetings.txt"

Response.Write "Welcome to Unauthorised Blog"

Response.End

Normally, the response header sent by a dynamic web page would be that of a standard HTML page. By specifying the application/text content type and adding the additional content disposition information, the web client now expect any subsequent data it receives to be the content of a greetings.txt file.

The same technique can be used to create any type of file, provided that you are able to create raw data for that particular file type.

No comments: