Products Technologies Demo Docs Blog Support Company

MVC: Autosave and Restore Documents to and from the Local Browser Storage

Web.TextControl documents are autosaved to browser localStorage at five-second intervals using the JavaScript saveDocument method. On page reload, the ribbonTabsLoaded event checks for stored content and restores it via loadDocument, displaying a notification bar to the user.

MVC: Autosave and Restore Documents to and from the Local Browser Storage

Modern HTML5-based web browsers support persistent data storage with an enhanced capacity up to 50MB local storage. It is possible to store data persistently or session based.

This sample shows how use the local storage to autosave documents and how to restore documents when the page is refreshed or reloaded.

MVC: Autosave and restore documents to and from the local browser storage

The following Javascript stores the document in an interval of 5 seconds and restores the document when the TX Text Control is initialized:

TXTextControl.addEventListener("ribbonTabsLoaded", function (e) {
    restoreDocument();
    var intervalAutoSave = setInterval(autoSaveDocument, 5000);
});

function autoSaveDocument() {
    TXTextControl.saveDocument(TXTextControl.streamType.InternalUnicodeFormat, function (e) {
        localStorage.document = e.data;
    });
}

function restoreDocument() {
    if (localStorage["document"]) {
        TXTextControl.loadDocument(
            TXTextControl.streamType.InternalUnicodeFormat,
            localStorage.document);

        $(".info").css("display", "inline-block");
    }
}

When the Web.TextControl is loaded completely, the ribbonTabsLoaded event is fired. In this event, the interval is created and a previously stored document is loaded.

The function autoSaveDocument uses the saveDocument method to store the document in the local storage variable document.

The function restoreDocument is loading the stored document back into TX Text Control using loadDocument. Finally, a green info bar is displayed to visualize that a document has been restored.

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET Server (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETGitHubHTML5

MVC: Loading Files from the Backstage Menu

An MVC partial backstage view lists available files from a directory using a simple document model. When a user clicks a file, JavaScript sends an AJAX request to an HttpPost controller method…


ASP.NETGitHubHTML5

MVC: Replace the File Menu with a Backstage View Menu

Replace the Web.TextControl File menu with a full-page backstage view using JavaScript and CSS. The tutorial disables the default FILE ribbon handler, renders a vertical navigation panel with…


ASP.NETGitHubHTML5

MVC: Replace the Ribbon Table Menu with a Quick Insert Table Drop-down

Replace the default Web.TextControl ribbon table button with a grid-based quick insert dropdown using JavaScript. The tutorial renders a visual row-and-column picker, sends the selected dimensions…


ASP.NETGitHubHTML5

MVC: Arrange a Docked Web.TextControl with a Custom Bar at the Top

Position a docked Web.TextControl below a custom toolbar bar in an MVC view by using CSS and JavaScript to offset the editor. The tutorial wraps the control in a DIV, sets absolute positioning…


ASP.NETGitHubHTML5

MVC: Loading and Saving Documents Through Controller HttpPost Methods

Web.TextControl X13 for ASP.NET MVC provides document load and save operations through controller HttpPost methods. The JavaScript API serializes documents as Base64, posts them via jQuery AJAX to…

Share on this blog post on: