With the release of TX Text Control X13, we released an MVC version of TXTextControl.Web that is available through NuGet.

This sample project shows how to load and save documents using controller methods and Javascript. The view contains the TXTextControl.Web HtmlHelper and two buttons to save and to load the document.

@using TXTextControl.Web
@using TXTextControl.Web.MVC
@{
ViewBag.Title = "Index";
}
@* Add TXTextControl.Web to the view *@
@Html.TXTextControl().TextControl().Render()
<input onclick="SaveToController('test.docx')" id="btnSave" type="button" value="Save" />
<input onclick="LoadFromController('test.docx')" id="btnLoad" type="button" value="Load" />
view raw Index.cshtml hosted with ❤ by GitHub

The Javascript function SaveToController uses the method saveDocument to save the document client-side in the internal Unicode format. Then, the jQuery AJAX method calls the controller method SaveTemplate by sending a JSON string that contains the document name and the saved document.

function SaveToController(DocumentName) {
TXTextControl.saveDocument(TXTextControl.streamType.InternalUnicodeFormat,
function (e) {
var serviceURL = "/Home/SaveTemplate";
$.ajax({
type: "POST",
url: serviceURL,
contentType: 'application/json',
data: JSON.stringify({
DocumentName: DocumentName,
BinaryDocument: e.data
}),
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
alert("Document has been saved successfully.");
}
function errorFunc() {
alert('Error');
}
});
}

The controller HttpPost method accepts the JSON string in form of a model in the parameter. The document is converted to a byte[] array and loaded into a temporary ServerTextControl instance. Finally, the document is saved server-side using the Save method of the ServerTextControl object.

[HttpPost]
public bool SaveTemplate(DocumentViewModel model)
{
string name = model.DocumentName;
byte[] document = Convert.FromBase64String(model.BinaryDocument);
using (TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load(document, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
tx.Save(Server.MapPath("/App_Data/documents/" + model.DocumentName),
TXTextControl.StreamType.WordprocessingML);
}
return true;
}
view raw HomeController.cs hosted with ❤ by GitHub

The model DocumentViewModel is shown in the code below:

public class DocumentViewModel
{
public string DocumentName { get; set; }
public string BinaryDocument { get; set; }
}

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