Earlier in the chapter you learned about the Response.Write() method that outputs strings to the HTML document. This might not always be the most convenient way to output text data to the page. Sometimes it's easier to send the contents of a file out, and the Response.WriteFile() method is exactly what you need to do this.

You can send a file out with the following syntax:

 Response.WriteFile( "File.txt" ) 

A demo program on www.UsingASP.net offers the user four selections. When the form is submitted to the WriteFileResult.aspx file, the value of the SelectFile form field is retrieved and the appropriate file is output. The following HTML code offers users a choice of files:

 <form method="POST" action="WriteFileResult.aspx">    Select a File:<br>    <select Name="SelectFile">      <option Value="File1.txt">Poem</option>      <option Value="File2.txt">Haiku</option>      <option Value="File3.txt">Limerick</option>      <option Value="File4.txt">Quote</option>    </select>    <input type="submit" value="Submit" name="B1">    <input type="reset" value="Reset" name="B2">  </form> 


This code (which has had the formatting tags removed here in the text) is part of the page that you can find at www.UsingASP.net. Select Chapter Examples, Chapter 4, then WriteFile. You can see the rendered page in Figure 4.8.

Figure 4.8. You can select a file to load.


The code to output the file into the HTML document is extremely simple:

 <%     Response.WriteFile( Request.Form( "SelectFile" ) )  %> 


This code is part of the page that is invoked by filling out the form shown in Figure 4.8 and clicking the Submit button. You can see the rendered page in Figure 4.9.

Figure 4.9. The file containing a Haiku was inserted.



Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net