How to export data from Context Menu to a text file

2 posts, 0 answers
  1. Doug
    Doug avatar
    1 posts
    Member since:
    May 2017

    Posted 21 Sep Link to this post

    I am showing a RadTreeView on my page and it has a context menu associated with each node.  When you right click on any node, an option is displayed called "Export".  This export will run a stored procedure and bring data from the database.  I want to export this date to a .txt file.  This is enforced by my business requirements so exporting it to Excel or PDF is not an option!!  I also need to be able to prompt the user for location of where they want to save this file and what they want to name it.  Does telerik have any controls that will help me accomplish these tasks?  I have looked into the ClientExportManager and the export functionalities of the RadGrid and TreeList, but nothing exports data to text file that I can see.

     

    thanks

     

    doug

  2. Peter Milchev
    Admin
    Peter Milchev avatar
    301 posts

    Posted 26 Sep Link to this post

    Hello Doug,

    The export to a Text file should be implemented manually.

    Here is a sample implementation you could use as a starting point in your application.

    <telerik:RadButton runat="server" ID="RadButton1" Text="Export to .txt" OnClick="RadButton1_Click"/>
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        var filename = "my filename.txt";
     
        // create your file on the fly.
        var txtBuilder = new StringBuilder();
        for (int n = 0; n <= 20; n = n + 2)
        {
            txtBuilder.AppendLine(n.ToString());
        }
     
        // make it as a stream
        var txtContent = txtBuilder.ToString();
        var txtStream = new MemoryStream(Encoding.UTF8.GetBytes(txtContent));
     
        // send it to client
        Response.ClearHeaders();
        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        Response.AddHeader("Content-Length", txtStream.Length.ToString());
        Response.ContentType = "text/plain";
        Response.Flush();
        Response.BinaryWrite(txtStream.ToArray());
        Response.End();
    }

    Regards,
    Peter Milchev
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top