MVC: Loading and Saving Documents Directly Server-Side
This article explains how to use the server-side WebSocketHandler to load and save document directly server-side.

The MVC version of TX Text Control provides a JavaScript API to load and save documents locally:
- Javascript: TXText
Control.load Document method - Javascript: TXText
Control.save Document method
These client-side methods are very useful when managing your data including documents client-side. In case you would like to store your documents server-side, you would need to save it locally in order to send it to the server again. For a saving process, the document is being sent twice using HTTP.
Because the document is already server-side (it gets synchronized with the document synchronization service), you can use a little trick to save the document directly server-side without the overhead of sending it back and forth.
The following code stores Text Control settings including the ConnectionID that can be used server-side to connect to the proper instance:
var txSettings;
function checkSettings()
{
// store Text Control settings
var settings = document.querySelector("#__txSettings");
txSettings = JSON.parse(settings.textContent);
}
checkSettings();
The following function calls a Web API endpoint with the given ConnectionID:
// this function loads a document directly server-side
function loadDocument(documentName)
{
// call the Web API endpoint LoadDocument with the
// stored, current ConnectionID
var serviceURL = "/api/TXDocument/LoadDocument?ConnectionID="
+ txSettings.ConnectionID +
"&DocumentName=" + documentName;
$.ajax({
type: "GET",
url: serviceURL,
contentType: 'application/json',
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
// set the loaded document name
loadedDocument = documentName;
}
function errorFunc() {
console.log("An error occured.");
}
}
In the controller code, the ConnectionID is used to retrieve the associated instance of the WebSocketHandler:
[System.Web.Http.HttpGet]
public HttpResponseMessage LoadDocument(string ConnectionID, string DocumentName)
{
// connect the WebSocketHandler with the ConnectionID
WebSocketHandler wsHandler = WebSocketHandler.GetInstance(ConnectionID);
// the document directly server-side
wsHandler.LoadText(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/" + DocumentName),
StreamType.WordprocessingML);
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
};
}
Then the LoadText method can be used to load the document directly server-side which updates the view in the HTML5 canvas automatically.
The following diagram shows the workflow for this scenario:
You can test this on your own by downloading the sample project from our GitHub repository.
Also See
This post references the following in the documentation:
- Javascript: TXText
Control.load Document Method - Javascript: TXText
Control.save Document Method
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2017 or better
- TX Text Control .NET Server (trial sufficient)
Related Posts
ASP.NETASP.NET CoreDocument Editor
ASP.NET Core: Loading Documents in the Document Editor
Learn how to load a document in the Document Editor using ASP.NET Core. This article explains how to load a document from a file or a byte array and how to load a document from client-side…
Loading Documents into the Document Viewer in React
This article explains how to load documents into the Document Viewer in React. It shows how to load documents from a base64 encoded string and a URL.
ASP.NETASP.NET CoreWebSocketHandler
How to Manipulate the Document using the WebSocketHandler
The WebSocketHandler acts as a proxy between the client-side JavaScript and the TCP synchronization service. However, the WebSocketHandler can be used directly on the server side to do document…
Getting Started: ServerTextControl and MailMerge with ASP.NET MVC (.NET…
This article shows how to use the TX Text Control ASP.NET ServerTextControl and MailMerge classes within an ASP.NET web application in Visual Studio 2022.
Getting Started: Document Viewer with ASP.NET MVC (.NET Framework)
This article shows how to use the TX Text Control ASP.NET document viewer within a .NET Framework web application in Visual Studio 2022.