Example - Loading and Saving

Documents can be loaded from files on the server, from memory variables or memory streams in various formats including DOC, DOCX, RTF, HTML and the internal TX Text Control format.

This example is part of the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

Language Project Location
C# %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Sample Explorer\
VB.NET %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Sample Explorer\

You will find the source code and the ASPX page of this sample in the Solution structure tree under: HTML5\loadingsaving.aspx

Example description

This demo shows how to load files from the server and how to upload and load files from the client.

Loading files asynchronously

When the sample Web Form is opened, a document is loaded automatically asynchronously on the Form_Load event:

[C#]
protected void Page_Load(object sender, EventArgs e)
{
	TextControl1.LoadTextAsync(Server.MapPath("/documents/template.docx"),
		TXTextControl.Web.StreamType.WordprocessingML);
}
[Visual Basic]
Protected Sub Page_Load(sender As Object, e As EventArgs)
	TextControl1.LoadTextAsync(Server.MapPath("/documents/template.docx"), TXTextControl.Web.StreamType.WordprocessingML)
End Sub

Loading server-side files

On clicking the button Load document from file, a document is loaded using the LoadText method:

[C#]
protected void btnLoadFromServer_Click(object sender, EventArgs e)
{
	// load a physical document from the server
	TextControl1.LoadText(Server.MapPath("/documents/sample.docx"), 
		TXTextControl.Web.StreamType.WordprocessingML);
}
[Visual Basic]
Protected Sub btnLoadFromServer_Click(sender As Object, e As EventArgs)
	' load a physical document from the server
	TextControl1.LoadText(Server.MapPath("/documents/sample.docx"), TXTextControl.Web.StreamType.WordprocessingML)
End Sub

Loading files from memory

The button Load document from memory loads a document into the non-UI ServerTextControl that saves the document into a byte array in the internal unicode format. This byte array is then loaded into the Web.TextControl:

[C#]
protected void btnLoadFromServerMemory_Click(object sender, EventArgs e)
{
	// open a document using a temporary instance of ServerTextControl
	// and save it into a byte array
	// load the document from the byte array into the Web editor
	using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
	{
		tx.Create();
		TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
		ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;

		tx.Load(Server.MapPath("/documents/template.docx"), 
			TXTextControl.StreamType.WordprocessingML, ls);

		byte[] data;

		tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
		TextControl1.LoadText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
	}
}
[Visual Basic]
Protected Sub btnLoadFromServerMemory_Click(sender As Object, e As EventArgs)
	' open a document using a temporary instance of ServerTextControl
	' and save it into a byte array
	' load the document from the byte array into the Web editor
	Using tx As New TXTextControl.ServerTextControl()
		tx.Create()
		Dim ls As New TXTextControl.LoadSettings()
		ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord

		tx.Load(Server.MapPath("/documents/template.docx"), TXTextControl.StreamType.WordprocessingML, ls)

		Dim data As Byte()

		tx.Save(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)
		TextControl1.LoadText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat)
	End Using
End Sub

Loading client-side files

The file upload control uploads a document in the binary formats DOC and DOCX. The content is stored in a byte array and loaded using the LoadText method:

[C#]
protected void AjaxFileUpload1_UploadComplete(object sender, 
	AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
	// load the uploaded document from a byte array
	byte[] data = e.GetContents();

	TXTextControl.Web.BinaryStreamType streamType = 
		TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat;

	if (e.FileName.EndsWith("doc") == true)
		streamType = TXTextControl.Web.BinaryStreamType.MSWord;
	else if (e.FileName.EndsWith("docx") == true)
		streamType = TXTextControl.Web.BinaryStreamType.WordprocessingML;
	else
		return;

	try
	{
		TextControl1.LoadText(data, streamType);
	}
	catch { }
}
[Visual Basic]
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)
	' load the uploaded document from a byte array
	Dim data As Byte() = e.GetContents()

	Dim streamType As TXTextControl.Web.BinaryStreamType = TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat

	If e.FileName.EndsWith("doc") = True Then
		streamType = TXTextControl.Web.BinaryStreamType.MSWord
	ElseIf e.FileName.EndsWith("docx") = True Then
		streamType = TXTextControl.Web.BinaryStreamType.WordprocessingML
	Else
		Return
	End If

	Try
		TextControl1.LoadText(data, streamType)
	Catch
	End Try
End Sub

Saving server-side files

When clicking the button Save document as a file on the server, the SaveText method is used to save a physical file server-side:

[C#]
protected void btnSaveToServer_Click(object sender, EventArgs e)
{
	// save the document to a physical file on the server
	string sFilename = "results_" + Guid.NewGuid().ToString() + ".docx";
	TextControl1.SaveText(Server.MapPath("/output/" + sFilename), 
		TXTextControl.Web.StreamType.WordprocessingML);
	lblStatusSave.Text = "Document has been saved:<br />" + sFilename;
}
[Visual Basic]
Protected Sub btnSaveToServer_Click(sender As Object, e As EventArgs)
	' save the document to a physical file on the server
	Dim sFilename As String = "results_" + Guid.NewGuid().ToString() + ".docx"
	TextControl1.SaveText(Server.MapPath(Convert.ToString("/output/") & sFilename), TXTextControl.Web.StreamType.WordprocessingML)
	lblStatusSave.Text = Convert.ToString("Document has been saved:<br />") & sFilename
End Sub

Saving files to memory

When the last button Save document to the memory on the server is clicked, the SaveText method is used to save the current document into a byte array in the internal unicode format. This byte array is loaded into an instance of the ServerTextControl:

[C#]
protected void btnSaveToServerMemory_Click(object sender, EventArgs e)
{
	// save the document into a byte array
	byte[] data;
	TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);

	// load the byte array into a temporary ServerTextControl
	// sample use case: store document in a database
	using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
	{
		tx.Create();
		tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
	}

	lblStatusSave0.Text = "Document has been temporary saved at " + 
		String.Format("{0:T}", DateTime.Now);
}
[Visual Basic]
Protected Sub btnSaveToServerMemory_Click(sender As Object, e As EventArgs)
	' save the document into a byte array
	Dim data As Byte()
	TextControl1.SaveText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat)

	' load the byte array into a temporary ServerTextControl
	' sample use case: store document in a database
	Using tx As New TXTextControl.ServerTextControl()
		tx.Create()
		tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)
	End Using

	lblStatusSave0.Text = "Document has been temporary saved at " + [String].Format("{0:T}", DateTime.Now)
End Sub