Web.TextControl 提供了加载和保存文档到服务器的后台API。另外,本地文档也可以同前台代码加载到TX提供的 HTML5 Viewer中进行展示。
在客户端,Javascript API 如下:
TXTextControl.loadDocument(streamType, base64Data);
这个例子展示了怎样使用 javascript 脚本加载本地文档:
点击 Input 按钮,选择预加载的文件, 文档本转换成 FileReader 类,readAsDataURL 方法转换文档为Base64 编码。
// *****************************************************************// readDocument//// Desc: This function reads a file, converts it to a Base64 encoded// string and loads it into TX Text Control//// param input: The input HTML element// *****************************************************************function readDocument(input) {if (input.files && input.files[0]) {var fileReader = new FileReader();fileReader.onload = function (e) {var streamType = TXTextControl.streamType.PlainText;// set the StreamType based on the lower case extensionswitch (fileinput.value.split('.').pop().toLowerCase()) {case 'doc':streamType = TXTextControl.streamType.MSWord;break;case 'docx':streamType = TXTextControl.streamType.WordprocessingML;break;case 'rtf':streamType = TXTextControl.streamType.RichTextFormat;break;case 'htm':streamType = TXTextControl.streamType.HTMLFormat;break;case 'tx':streamType = TXTextControl.streamType.InternalUnicodeFormat;break;case 'pdf':streamType = TXTextControl.streamType.AdobePDF;break;}// load the document beginning at the Base64 data (split at comma)TXTextControl.loadDocument(streamType, e.target.result.split(',')[1]);};// read the file and convert it to Base64fileReader.readAsDataURL(input.files[0]);}}// call readDocument when a new document has been selected$("#fileinput").change(function () {readDocument(this);});
下载代码体验该功能:
更多TX功能请参考: