Microsoft KB Archive/300108

From BetaArchive Wiki

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

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

  1. 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

  2. From the Project Explorer in Visual InterDev, right-click <MyWebServer>/<projectname>, point to Add, and then click Active Server Page. Name the new ASP page DynamicText.asp.
  3. 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("buttonSubmit") <> "" then
    
        Dim fileName
        Dim str1
        Dim str2
    
        fileName = "DynamicText.txt"
            'These are the strings that are used to build the text file.
        str1 = "<-- Hello -->"
        str2 = "This is a demo on creating dynamic text file"
    
            'ContentType specifies the MIME type of this header.
        Response.ContentType = "text/plain" 
            'The AddHeader method adds an HTML header with a specified value. 
            'Content-disposition forces the browser to download.
        Response.AddHeader "content-disposition", "attachment; filename=""" & fileName & """"
        Response.Write str1 & vbnewline
        Response.Write str2
        Response.End
        
    End if
    
    %>
    
    <HTML>
    <HEAD>
    <TITLE> Demo on Creating Dynamic Text File </TITLE>
    </HEAD>
    
    <BODY>
    <h4> This is a demo on creating a dynamic text file </h4>
    
    <FORM action="dynamicText.asp" method=POST id=form1 name=form1>
    <INPUT type="submit" value="Download TextFile" id=buttonSubmit name=buttonSubmit>
    </FORM>
    
    </BODY>
    </HTML>
                        
  4. 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.
  5. 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

  6. When the ASP page is loaded, click Download TextFile. The file is then sent to the browser.

back to the top

Code Explanation

  • The following code specifies the ContentType property of the HTTP header:

    Response.ContentType = "text/plain"
                            

    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 "text/plain" because the header contains text data. If you do not specify the content type, Microsoft Internet Information Server (IIS) defaults the content type to "application/octlet-stream".

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

  • The "content-disposition" header forces the client browser to prompt the file download:

    Response.AddHeader "content-disposition", "attachment; filename=""" & fileName & """"
                            

    If you want the browser to handle the text content instead of prompting the user to download, you can remove the "content-disposition" 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 "content-disposition" 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:

  • The following code writes the text content (the "str1" and "str2" strings) by Response.Write:

    Response.Write str1 & vbnewline
    Response.Write str2
    Response.End
                            

    "vbnewline" 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 "application/msexcel". 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

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 "application/octet-stream" because some Web browsers handle the "text/plain" type instead of prompting for file download.

back to the top

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

260519 How To Raise a "File Download" Dialog Box for a Known MIME Type


Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.


back to the top

Keywords: kbhowto kbhowtomaster KB300108