Create text file and download using asp.net

In this article I am going to explain how to create text file and download using asp.net


Implementation:
I want to create text file and download it on button click. I have created example to fulfill this requirement.

Complete HTML Markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table>
    <tr>
     <td>Enter Text :</td>
    <td> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="254px"
            Width="378px"></asp:TextBox></td></tr>
     <tr>
     <td></td>
    <td></td></tr>
     <tr>
     <td></td>
    <td><asp:Button ID="Button1"
            runat="server" Text="Create Text File" Height="26px" /></td></tr>
    </table>
    </div>
    </form>
</body>
</html>


C# Code:
protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string text = TextBox1.Text;
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Length", text.Length.ToString());
            Response.ContentType = "text/plain";
            Response.AppendHeader("content-disposition", "attachment;filename=\"aspmantra.txt\"");
            Response.Write(text);
            Response.End();
        }
        catch (Exception ex) { }
    }
  

VB.net Code:
  Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim text As String = TextBox1.Text
            Response.Clear()
            Response.ClearHeaders()
            Response.AddHeader("Content-Length", text.Length.ToString())
            Response.ContentType = "text/plain"
            Response.AppendHeader("content-disposition", "attachment;filename=""aspmantra.txt""")
            Response.Write(text)
            Response.[End]()
        Catch ex As Exception
        End Try
    End Sub



Post a Comment

Hi, I am thankful to you for sharing this awesome article with this helpful knowledge. this is the blog that provide the lots of good information thanks for provide such a good information.

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget