Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to generate a text file via ASP.NET & make it avail. for download?

Status
Not open for further replies.

sa5cha

Programmer
May 29, 2001
34
DE
Hi,

I need to generate a simple XML File on the fly via ASP.NET. The user should be able to download that file to his local harddrive.

I just need the mechanism how to generate a downloadable file ? Is that possible ? Please point me in the right direction or post an VB.NET example.

Thank you in advance.

Sascha
 
use a generic handler.
write the data to an xmlwriter.
output the contents of the writer to the response.stream.
add the appropiate headers so the user can down load the file. (by default xml is displayed. you will need to tell the browser to download the file).

all this is available on the web. probably in pieces.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason,

thank you for your help. I managed to write the code.

Here's an example for others, just copy&pasted it into an empty aspx Webpage.


Code:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                                       
        Dim MyXMLContent As New XmlTextWriter(Server.MapPath("xml/demo.xml"), Encoding.UTF8)
        
        MyXMLContent.WriteStartDocument()
        
        MyXMLContent.WriteComment("#########################################")
        MyXMLContent.WriteComment("MyComment")
        MyXMLContent.WriteComment("#########################################")
        MyXMLContent.WriteComment("Timestamp:" & Now)
        
        MyXMLContent.WriteStartElement("Element1")
        MyXMLContent.WriteStartElement("Element2")
        MyXMLContent.WriteElementString("ElementStr1", "ElementStr1_Val")
        MyXMLContent.WriteElementString("ElementStr2", "ElementStr2_Val")
        'Close Element2
        MyXMLContent.WriteEndElement()
                
        MyXMLContent.WriteStartElement("Element3")
        MyXMLContent.WriteElementString("ElementStr3", "ElementStr3_Val")
        MyXMLContent.WriteElementString("ElementStr4", "ElementStr4_Val")
        'Close Element3
        MyXMLContent.WriteEndElement()
        
        'Close Element1
        MyXMLContent.WriteEndElement()
        
        MyXMLContent.WriteEndDocument()

        MyXMLContent.Flush()
        MyXMLContent.Close()
    End Sub
</script>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title>XML Writer - Demo</title>
    <script type="text/javascript">
    
      function pageLoad() {
      }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <a href="xml/demo.xml">Download Demo XML</a>
    </div>
    </form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top