MVC: Loading a Document in the View Code from a MemoryStream
The LoadText method in Web.TextControl accepts files, streams, strings, and byte arrays. In an MVC controller, a document is loaded into a MemoryStream and encoded as a Base64 string in the ViewModel. The view decodes the string back to a byte array and calls LoadText to display it.

The LoadText method accepts a physical file, a FileStream, a string or a byte array to load documents into the web editor.
This sample shows how to load a document in the controller and pass it as a ViewModel to the view in order to load a document.
In the controller method Index, the document is loaded into a MemoryStream and then stored in the ViewModel Document that holds the document as a Base64 encoded string. This ViewModel is returned to the actual view.
public ActionResult Index()
{
Document document = new Models.Document();
MemoryStream ms = new MemoryStream();
FileStream fs;
// load the document into a stream
using (fs = System.IO.File.OpenRead(
Server.MapPath("/App_Data/documents/document.docx")))
{
fs.CopyTo(ms);
document.BinaryDocument = Convert.ToBase64String(ms.ToArray());
}
// forward the document to the view
return View(document);
}
In the view, the Base64 encoded string is converted back into a byte array and loaded using the LoadText method into the web editor.
@model tx_loadStream.Models.Document
@using TXTextControl.Web
@using TXTextControl.Web.MVC
@Html.TXTextControl().TextControl(settings => {
settings.Dock = TXTextControl.Web.DockStyle.Window;
}
).LoadText(Convert.FromBase64String(Model.BinaryDocument),
BinaryStreamType.WordprocessingML).Render()
Download the sample from GitHub and test it on your own.
![]()
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 2015 or better
- TX Text Control .NET Server (trial sufficient)
Related Posts
Updated MVC Sample: Loading Files from the Backstage Menu
The ASP.NET MVC backstage menu sample has been updated to TX Text Control X14 with the latest NuGet packages. The backstage view replaces the default file menu with a customizable, MS Word-style…
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…
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…
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…
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…
