Create text file and download using asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial


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



What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+

1 comments:

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.


EmoticonEmoticon