Microsoft KB Archive/300108

= HOW TO: Send a Dynamic Text Document to the Client by Using Active Server Pages =

Article ID: 300108

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q300108



IN THIS TASK
SUMMARY
 * Building the Page
 * Code Explanation
 * Troubleshooting

REFERENCES



SUMMARY
This article provides step-by-step instructions on how to use Active Server Pages (ASP) to send a dynamic text document to the client. This is useful when you have string data that is stored on a database and want to send the data to the client browser without creating the actual files on the Web server.

This sample consists of a form that posts to itself. When you click the submit button, the server-side script code writes out the data (text) in the Hypertext Transfer Protocol (HTTP) header to the client.

back to the top

Building the page
 Create a new Web project in Visual InterDev 6.0. For detailed instructions, see the following article in the Microsoft Knowledge Base:

301184 HOW TO: Create a Visual InterDev Project

 From the Project Explorer in Visual InterDev, right-click /, point to Add, and then click Active Server Page. Name the new ASP page DynamicText.asp.  In the Source window, delete all of the code that Visual InterDev generates. Highlight the following code, right-click the code, and then click Copy. In Source window of Visual InterDev, click Paste as HTML on the Edit menu to paste the following code: <%@ Language=VBScript %>

<%

if Request.Form(&quot;buttonSubmit&quot;) <> &quot;&quot; then

Dim fileName Dim str1 Dim str2

fileName = &quot;DynamicText.txt&quot; 'These are the strings that are used to build the text file. str1 = &quot;<-- Hello -->&quot; str2 = &quot;This is a demo on creating dynamic text file&quot;

'ContentType specifies the MIME type of this header. Response.ContentType = &quot;text/plain&quot; 'The AddHeader method adds an HTML header with a specified value. 'Content-disposition forces the browser to download. Response.AddHeader &quot;content-disposition&quot;, &quot;attachment; filename=&quot;&quot;&quot; & fileName & &quot;&quot;&quot;&quot; Response.Write str1 & vbnewline Response.Write str2 Response.End End if

%>

   Demo on Creating Dynamic Text File  

<BODY> This is a demo on creating a dynamic text file

<FORM action=&quot;dynamicText.asp&quot; method=POST id=form1 name=form1> <INPUT type=&quot;submit&quot; value=&quot;Download TextFile&quot; id=buttonSubmit name=buttonSubmit> </FORM>

</BODY> </HTML> </li> You can also use Notepad to create this ASP page. From Notepad, paste the preceding code into a new document. From the File menu, click Save. In the Save As dialog box, in the Save In drop-down list box, browse to the root location of your Web server (which is typically C:\InetPub\Wwwroot). In the File name drop-down list box, type DynamicText.asp. In the Save as type drop-down list box, click All Files. Finally, click Save.</li> To view the page, in Visual InterDev, right-click in the ASP page, and then click View In Browser. Alternately, you can open your browser and type the following address in the Address bar:

http://<servername>/DynamicText.asp

</li> When the ASP page is loaded, click Download TextFile. The file is then sent to the browser.</li></ol>

back to the top

Code Explanation
<ul>  The following code specifies the ContentType property of the HTTP header: Response.ContentType = &quot;text/plain&quot; This informs the browser what type of data is being sent to the browser. The code sample in this article sets the content type to &quot;text/plain&quot; because the header contains text data. If you do not specify the content type, Microsoft Internet Information Server (IIS) defaults the content type to &quot;application/octlet-stream&quot;.

For more information about the ContentType property, see Request for Comments (RFC) 1341 at the following World Wide Web Consortium (W3C) Web site:

http://www.w3.org/Protocols/rfc1341/7_1_Text.html

</li>  The &quot;content-disposition&quot; header forces the client browser to prompt the file download: Response.AddHeader &quot;content-disposition&quot;, &quot;attachment; filename=&quot;&quot;&quot; & fileName & &quot;&quot;&quot;&quot; If you want the browser to handle the text content instead of prompting the user to download, you can remove the &quot;content-disposition&quot; line of code.

You can also specify content-disposition on the IIS server. To apply the header statically, right-click the document in the Internet Service Manager, and then click Properties. On the HTTP Headers tab, type the &quot;content-disposition&quot; header. This works best when you want to apply content-disposition to only a few files on your system and do not require the overhead of ASP.

For more information about content-disposition, see RFC 1806 at the following Ohio State University Web site:

http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1806.html

</li>  The following code writes the text content (the &quot;str1&quot; and &quot;str2&quot; strings) by Response.Write: Response.Write str1 & vbnewline Response.Write str2 Response.End &quot;vbnewline&quot; is a constant from Visual Basic Scripting Edition (VBScript) that contains the character values of carriage return/line feed (CR/LF). When str1 and str2 are written out to the HTTP header, a CR/LF is written out between the two strings. The sample code in this article uses Response.End to ensure that no content is sent to the client after this line.

NOTE: You may modify this code to write out a binary file instead of text content. If you do so, you must change the ContentType property. For example, if you write out a Microsoft Excel file, set ContentType to &quot;application/msexcel&quot;. In addition, you must use Response.BinaryWrite instead of Response.Write.For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

276488 How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP

</li></ul>

back to the top

Troubleshooting
For additional information about the potential pitfalls when you use content-disposition, click the article numbers below to view the articles in the Microsoft Knowledge Base:

182315 FIX: Content-Disposition: Does Not Force File Download Dialog

279667 BUG: Content-Disposition Attachment Headers Does Not Save File

In other browsers, you may want to set the content-disposition to &quot;application/octet-stream&quot; because some Web browsers handle the &quot;text/plain&quot; type instead of prompting for file download.

back to the top

<div class="references_section">