This tutorial explains how to load and save documents from and to a server.
The source code for this sample program is contained in the following directories:
- Samples\ASP.NET\CSharp\Browser\LoadSaveHTTP
- Samples\ASP.NET\VisualBasic\Browser\LoadSaveHTTP
Used TX Text Control controls:
- TXTextControl.TextControl
- TXTextControl.ServerTextControl
- TXTextControl.ButtonBar
- TXTextControl.RulerBar
- TXTextControl.StatusBar
Relevant API links:
- TextControl.Load
- TextControl.Save
1. Preparing the user control
To load a document from the server, the ServerTextControl class is used server-side to convert the document to TX Text Control format. This data is then passed on to the browser. Client-side, the document is loaded using a TextControl.
1. Create a simple ASP.NET application as described in the previous chapter (except step 3 and 5).
2. Insert a private byte variable with the name m_document and the public property Document. The BrowserAppControl class should look like this:
[C#]
public partial class BrowserAppControl : UserControl
{
private byte[] m_document;
public BrowserAppControl()
{
InitializeComponent();
textControl1.ButtonBar = buttonBar1;
textControl1.RulerBar = rulerBar1;
textControl1.VerticalRulerBar = rulerBar2;
textControl1.StatusBar = statusBar1;
textControl1.CreateControl();
}
public byte[] Document
{
get
{
TXTextControl.SaveSettings sSettings = new TXTextControl.SaveSettings();
textControl1.Save(out m_document, sSettings);
return m_document;
}
set
{
m_document = value;
TXTextControl.LoadSettings lSettings = new TXTextControl.LoadSettings();
textControl1.Load(m_document, lSettings);
}
}
}
[Visual Basic]
Public Class BrowserAppControl
Dim m_document() As Byte
Public Sub New()
InitializeComponent()
TextControl1.ButtonBar = ButtonBar1
TextControl1.RulerBar = RulerBar1
TextControl1.VerticalRulerBar = RulerBar2
TextControl1.StatusBar = StatusBar1
TextControl1.ViewMode = TXTextControl.ViewMode.PageView
TextControl1.CreateControl()
End Sub
Public Property Document() As Byte()
Get
Dim sSettings As TXTextControl.SaveSettings = New TXTextControl.SaveSettings()
TextControl1.Save(m_document, sSettings)
Return m_document
End Get
Set(ByVal value As Byte())
m_document = value
Dim lSettings As TXTextControl.LoadSettings = New TXTextControl.LoadSettings()
TextControl1.Load(m_document, lSettings)
End Set
End Property
End Class
The property implemented above is able to load and save a document from and to a byte array in the internal TX Text Control Unicode format.
2. Preparing the code-behind
1. Select the BrowserWebSite project in the Solution Explorer and choose Add New Item... from theProject main menu. Choose Web Form as the template, type GetDocument.aspx as the name and confirm with Add.
2. Repeat step 1 with SetDocument.aspx as the web form's name.
3. Open the code-behind file of the GetDocument.aspx Web Form (GetDocument.aspx.cs orGetDocument.aspx.vb). Insert the following code into the Page_Load event, so that is looks like this:
[C#]
protected void Page_Load(object sender, EventArgs e)
{
TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl();
tx.Create();
tx.Load(Server.MapPath("Demo.rtf"), TXTextControl.StreamType.RichTextFormat);
byte[] temp;
tx.Save(out temp, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
tx.Dispose();
Response.Clear();
Response.Expires = 0;
Response.BinaryWrite(temp);
}
[Visual Basic]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tx As TXTextControl.ServerTextControl = New TXTextControl.ServerTextControl()
tx.Create()
tx.Load(Server.MapPath("Demo.rtf"), TXTextControl.StreamType.RichTextFormat)
Dim temp() As Byte = Nothing
tx.Save(temp, TXTextControl.BinaryStreamType.InternalUnicodeFormat)
tx.Dispose()
Response.Clear()
Response.Expires = 0
Response.BinaryWrite(temp)
End Sub
4. Repeat step 3 with the SetDocument.aspx.cs or SetDocument.aspx.vb file and this code:
[C#]
protected void Page_Load(object sender, EventArgs e)
{
TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl();
tx.Create();
byte[] data = new byte[Page.Request.InputStream.Length];
Page.Request.InputStream.Read(data, 0, (int)Page.Request.InputStream.Length);
tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
tx.Save(Server.MapPath("Demo.rtf"), TXTextControl.StreamType.RichTextFormat);
tx.Dispose();
}
[Visual Basic]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tx As TXTextControl.ServerTextControl = New TXTextControl.ServerTextControl()
tx.Create()
Dim data(Page.Request.InputStream.Length) As Byte
Page.Request.InputStream.Read(data, 0, Page.Request.InputStream.Length)
tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)
tx.Save(Server.MapPath("Demo.rtf"), TXTextControl.StreamType.RichTextFormat)
tx.Dispose()
End Sub
5. Select the BrowserWebSite project in the Solution Explorer and choose Add Existing File... from the Project main menu.
Browse for the tx.js Javascript file. It is located in the samples folder:
C:\Program Files\Text Control GmbH\TX Text Control 18.0.NET\Samples\ASP.NET\CSharp\Browser\Basics\tx.js
6. Switch to the HTML view of the Default.aspx. Add the following code under the <object></object>tag block of the container user control:
<input type="button" id="button1" value="Load" onclick="return button1_onclick()"/> <input type="button" id="button2" value="Save" onclick="return button2_onclick()"/>
7. Add these script blocks to the HTML header (<head> tag).
<script language="javascript" src="tx.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> <!-- function button1_onclick() { var file = GetFile('GetDocument.aspx'); document.getElementById('BrowserApp').Document = file; } function button2_onclick() { var data; data = document.getElementById('BrowserApp').Document; SendFile('SetDocument.aspx',data); } // --> </script>
8. Copy the Demo.rtf from the samples folder to the root folder of the BrowserWebSite. The Demo.rtfis located here:
C:\Program Files\Text Control GmbH\TX Text Control 18.0.NET\Samples\Demo\Demo.rtf
9. Add a ServerTextControl reference to the project by dropping a ServerTextControl from the toolbox to the Component Designer View of the Default.aspx. This is shown in the ASP.NET User's Guide.
10. Ensure that all steps in this tutorial chapter are done:
3. Running the Web Application
11. Press F5 to compile and start the application.
No comments:
Post a Comment