# 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.

- **Author:** Bjoern Meyer
- **Published:** 2016-03-16
- **Modified:** 2026-07-17
- **Description:** 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.
- **2 min read** (202 words)
- **Tags:**
  - GitHub
  - HTML5
  - MVC
- **Web URL:** https://www.textcontrol.com/blog/2016/03/16/mvc-loading-a-document-in-the-view-code-from-a-memorystream/
- **LLMs URL:** https://www.textcontrol.com/blog/2016/03/16/mvc-loading-a-document-in-the-view-code-from-a-memorystream/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2016/03/16/mvc-loading-a-document-in-the-view-code-from-a-memorystream/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.Web.MVC.LoadStream

---

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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Updated MVC Sample: Loading Files from the Backstage Menu](https://www.textcontrol.com/blog/2017/04/20/updated-mvc-sample-loading-files-from-the-backstage-menu/llms.txt)
- [MVC: Loading Files from the Backstage Menu](https://www.textcontrol.com/blog/2016/01/06/mvc-loading-files-from-the-backstage-menu/llms.txt)
- [MVC: Replace the File Menu with a Backstage View Menu](https://www.textcontrol.com/blog/2015/12/30/mvc-replace-the-file-menu-with-a-backstage-view-menu/llms.txt)
- [MVC: Replace the Ribbon Table Menu with a Quick Insert Table Drop-down](https://www.textcontrol.com/blog/2015/12/23/mvc-replace-the-ribbon-table-menu-with-a-quick-insert-table-drop-down/llms.txt)
- [MVC: Arrange a Docked Web.TextControl with a Custom Bar at the Top](https://www.textcontrol.com/blog/2015/12/18/mvc-arrange-a-docked-webtextcontrol-with-a-custom-bar-at-the-top/llms.txt)
- [MVC: Autosave and Restore Documents to and from the Local Browser Storage](https://www.textcontrol.com/blog/2015/12/14/mvc-autosave-and-restore-documents-to-and-from-the-local-browser-storage/llms.txt)
- [MVC: Loading and Saving Documents Through Controller HttpPost Methods](https://www.textcontrol.com/blog/2015/12/08/mvc-loading-and-saving-documents-through-controller-httppost-methods/llms.txt)
- [HTML5: Saving Documents in an MVC Controller Method](https://www.textcontrol.com/blog/2015/10/01/html5-saving-documents-in-an-mvc-controller-method/llms.txt)
- [Web.TextControl: JQueryUI Alert Boxes and Javascript Events](https://www.textcontrol.com/blog/2015/04/20/webtextcontrol-jqueryui-alert-boxes-and-javascript-events/llms.txt)
- [Detect Toggle Button Changes Using a MutationObserver](https://www.textcontrol.com/blog/2021/11/11/detect-toggle-button-changes-using-a-mutationobserver/llms.txt)
- [Implementing Conditional Table Cell Colors with MailMerge](https://www.textcontrol.com/blog/2020/10/08/implementing-conditional-table-cell-colors-with-mailmerge/llms.txt)
- [Deploying the MVC HTML5 Editor to Azure App Services](https://www.textcontrol.com/blog/2018/04/06/deploying-the-mvc-html5-editor-to-azure-app-services/llms.txt)
- [Sneak Peek X15: Copy to Local Clipboard Support in ASP.NET Version](https://www.textcontrol.com/blog/2017/07/13/sneak-peek-x15-copy-to-local-clipboard-support-in-aspnet-version/llms.txt)
- [Sneak Peek X15: Custom Field Overlays in HTML5-based Text Control](https://www.textcontrol.com/blog/2017/06/21/sneak-peek-x15-custom-field-overlays-in-html5-based-text-control/llms.txt)
- [New ASP.NET MVC DocumentViewer Rolled out to the ReportingCloud Portal](https://www.textcontrol.com/blog/2017/04/22/new-aspnet-mvc-documentviewer-rolled-out-to-the-reportingcloud-portal/llms.txt)
- [ASP.NET MVC: Implementing a Simplistic, Custom Button Bar](https://www.textcontrol.com/blog/2017/03/13/aspnet-mvc-implementing-a-simplistic-custom-button-bar/llms.txt)
- [ASP.NET MVC: Adding Protected Sections to Documents](https://www.textcontrol.com/blog/2017/03/01/aspnet-mvc-adding-protected-sections-to-documents/llms.txt)
- [ASP.NET: Adding Electronic Signatures to Documents](https://www.textcontrol.com/blog/2016/10/12/aspnet-adding-electronic-signatures-to-documents/llms.txt)
- [Using an Azure Load Balancer with Web.TextControl](https://www.textcontrol.com/blog/2016/03/22/using-an-azure-load-balancer-with-webtextcontrol/llms.txt)
- [Setting the Interface and Control's Culture of Web.TextControl](https://www.textcontrol.com/blog/2015/12/16/setting-the-interface-and-controls-culture-of-webtextcontrol/llms.txt)
- [ASP.NET MVC: How to Use the HtmlHelper Settings with Lambda Expressions](https://www.textcontrol.com/blog/2015/12/10/aspnet-mvc-how-to-use-the-htmlhelper-settings-with-lambda-expressions/llms.txt)
- [HTML5: Adding a Download Button to the Ribbon File Menu](https://www.textcontrol.com/blog/2015/10/22/html5-adding-a-download-button-to-the-ribbon-file-menu/llms.txt)
- [HTML5: Make Merge Field Lists Scrollable in the Ribbon Bar](https://www.textcontrol.com/blog/2015/10/16/html5-make-merge-field-lists-scrollable-in-the-ribbon-bar/llms.txt)
- [HTML5: Store Documents Using the Local Browser Storage](https://www.textcontrol.com/blog/2015/10/09/html5-store-documents-using-the-local-browser-storage/llms.txt)
- [Sending Authorized Requests from WebForms to MVC](https://www.textcontrol.com/blog/2015/10/05/sending-authorized-requests-from-webforms-to-mvc/llms.txt)
