Products Technologies Demo Docs Blog Support Company

MVC: Loading a Document in the View Code from a MemoryStream

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. In the view, the Base64…

MVC: Loading a Document in the View Code from a MemoryStream

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.

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 2015 or better
  • TX Text Control .NET Server (trial sufficient)

Related Posts

ReportingGitHubHTML5

Updated MVC Sample: Loading Files from the Backstage Menu

We just updated the very popular sample Loading files from the backstage menu to TX Text Control version X14 (24.0) on GitHub. This sample shows how to replace the file menu with an MS Word-style…


ASP.NETGitHubHTML5

MVC: Loading Files from the Backstage Menu

Happy New Year, everybody! In the last blog entry, we showed how to replace the file menu with an MS Word-style backstage menu. This project shows how to load documents from a partial view in the…


ASP.NETGitHubHTML5

MVC: Replace the File Menu with a Backstage View Menu

Microsoft Word provides a very smart way to manage documents and related data such as metadata and personal information in a separate view: The backstage view. The ribbon bar contains commands for…


ASP.NETGitHubHTML5

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

MS Word provides a quick insert table drop-down to insert tables into the document. This sample shows how to replace the insert table button with such a table insert drop-down. The replacement and…


ASP.NETGitHubHTML5

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

Consider the following task: The Web.TextControl should be arranged under a custom bar that is located at the top of the page like in the following screenshot: Objects such as DIV elements doesn't…