It is much easier than you think. Keep in mind that the HTTP protocol does not actually transfer “files” in the strict sense of the word. It conveys requests and responses, each of which contains headers and content. In this case, you are concerned about the headers and content of the response.
The easiest way to do this in a WebForms application is through a generic handler . In particular, look at the implementation of the handler response in this link:
context.Response.ContentType = "image/png"; context.Response.WriteFile("~/Flower1.png");
This writes the contents of the image file in response after the response header is configured correctly. What you want is closer to what was commented on in the implementation:
context.Response.ContentType = "text/plain"; context.Response.Write("Hello World");
This will send plain text to the browser, nothing more. The browser does not believe that this is a web page or something like that, will not apply any styles to it, etc. As for the web browser, it just uploaded a text file with the words "Hello World" in it, you can Response.Write() all the text you want to create for this file.
You can also customize response headers to provide the browser with even more information. For example, if you added the following header to the HttpResponse :
Content-Disposition: attachment; filename=myfile.txt
The browser will then translate this to mean that this “file” must be downloaded and saved, and not just displayed. (Of course, the user's browser settings may say that he will still display it, but this is the right way for the server to “suggest” the browser to save the file.)
From the point of view of the browser, it does not matter where the “file” came from. Regardless of whether it was from the server's file system or dynamically generated or magically conjured, it does not matter. The browser only deals with response headers and content. If the headings say this is text, and say it is a file, then the content will be treated as a text file.