HTML5 Web Editor - Loading and Saving documents

Documents can be loaded from files on the server, from memory variables or memory streams in various formats including DOC, DOCX, RTF, HTML and the internal TX Text Control format.

This demo shows how to load files from the server and how to upload and load files from the client.

Demo

Loading documents Saving documents



The HTML5 Web editor uses WebSockets as the communication layer with the server. A WebSocket connection is always open and must not be disconnected by PostBacks. Therefore, all buttons and ASP.NET controls, except TextControl must be embedded in an AJAX UpdatePanel.

Sources

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tx_aspnet_samples.HTML5
{
    public partial class loadingsaving : System.Web.UI.Page
    {
        protected void btnLoadFromServer_Click(object sender, EventArgs e)
        {
            // load a physical document from the server
            TextControl1.LoadText(Server.MapPath("/documents/sample.docx"), 
                TXTextControl.Web.StreamType.WordprocessingML);
        }

        protected void btnLoadFromServerMemory_Click(object sender, EventArgs e)
        {
            // open a document using a temporary instance of ServerTextControl
            // and save it into a byte array
            // load the document from the byte array into the Web editor
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
                ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;

                tx.Load(Server.MapPath("/documents/template.docx"), 
                    TXTextControl.StreamType.WordprocessingML, ls);

                byte[] data;

                tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
                TextControl1.LoadText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
            }
        }

        protected void AjaxFileUpload1_UploadComplete(object sender, 
            AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            // load the uploaded document from a byte array
            byte[] data = e.GetContents();

            TXTextControl.Web.BinaryStreamType streamType = 
                TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat;

            if (e.FileName.EndsWith("doc") == true)
                streamType = TXTextControl.Web.BinaryStreamType.MSWord;
            else if (e.FileName.EndsWith("docx") == true)
                streamType = TXTextControl.Web.BinaryStreamType.WordprocessingML;
            else
                return;

            try
            {
                TextControl1.LoadText(data, streamType);
            }
            catch { }
        }

        protected void btnSaveToServer_Click(object sender, EventArgs e)
        {
            // save the document to a physical file on the server
            string sFilename = "results_" + Guid.NewGuid().ToString() + ".docx";
            TextControl1.SaveText(Server.MapPath("/output/" + sFilename), 
                TXTextControl.Web.StreamType.WordprocessingML);
            lblStatusSave.Text = "Document has been saved:<br />" + sFilename;
        }

        protected void btnSaveToServerMemory_Click(object sender, EventArgs e)
        {
            // save the document into a byte array
            byte[] data;
            TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);

            // load the byte array into a temporary ServerTextControl
            // sample use case: store document in a database
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
            }

            lblStatusSave0.Text = "Document has been temporary saved at " + 
                String.Format("{0:T}", DateTime.Now);
        }
    }
}

HTML5 Web editor