How to preview Word documents in .NET Web Forms FileExplorer?
By default, the FileExplorer control supports previewing image files only. However, you can preview Word documents using the RTE control. Initially, pass the document path to the AJAX handling “Importing” method that is available in the controller part in the “BeforeOpen” event of FileExplorer. The “Importing” method processes the word document and returns the document content to the AJAX success method, which will be passed to the “setHtml” method of RTE control. Refer to the following code sample.
ASP.NET
[View page]
C#
<ej:FileExplorer ID="fileexplorer" runat="server" CssClass="myFileBrowser" IsResponsive="true" Width="100%" AjaxAction="FileExplorerFeatures.aspx/FileActionDefault" Path="~/content/images/FileExplorer/" ClientSideOnBeforeOpen="beforeOpen" Layout="Tile"> </ej:FileExplorer> // The Dialog component <ej:Dialog ID="dialog" runat="server" ShowOnInit="false" ClientIDMode="Static" MaxWidth="100%" IsResponsive="true" EnableResize="false" Width="950px" Height="470px"> <DialogContent> // The RTE component is imported inside the dialog control for previewing Word files <ej:RTE ID="RTE" runat="server" ToolsList="importExport" AllowEditing="false" ShowToolBar="false" MinWidth="200px" Width="900px"> <RTEContent> <div> </div> </RTEContent> <Tools importExport="import"> </Tools> </ej:RTE> </DialogContent> </ej:Dialog> <script> // FileExplorer beforeOpen event function function beforeOpen(args) { if (args.itemType == "File" && (/\.(docx)$/i).test(this._selectedFile)) { $.ajax({ url: "FileExplorerFeatures.aspx/Importing", type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", data: '{ FilePath: "'+ args.path +'" }', success: function (data) { var rte = $("#RTE").ejRTE("instance"); // Set the data returned from controller to RTE through the setHtml method rte.setHtml(data.d); var dialogObj = $("#dialog").ejDialog("instance"); dialogObj.option("title", args.model.selectedItems[0]); // Open the dialog component dialogObj.open(); } }); } } </script>
[Controller]
C#
[System.Web.Services.WebMethod] // Function to import Word document into the RTE component public static string Importing(string FilePath) { string HtmlString = string.Empty; string path = System.Web.HttpContext.Current.Server.MapPath(FilePath); if (path != null) { using (var mStream = new MemoryStream()) { new WordDocument(path).Save(mStream, FormatType.Html); mStream.Position = 0; HtmlString = new StreamReader(mStream).ReadToEnd(); }; HtmlString = ExtractBodyContent(HtmlString); foreach (var item in DecodeKeys()) { HtmlString = HtmlString.Replace(item.Key, item.Value); } } return JsonConvert.SerializeObject(HtmlString); } public static IDictionary<string, string> DecodeKeys() { IDictionary<string, string> KeyValuePair = new Dictionary<string, string>() { {"\"", "'"},{"\r", " "},{"\n", "<br/> "},{"\r\n", " "},{"( )+", " "},{" ", " "},{"•", "*"},{"‹", "<"}, {"›", ">"},{"™", "(tm)"},{"©", "(c)"},{"®", "(r)"} }; return KeyValuePair; } public static string ExtractBodyContent(string html) { if (html.Contains("<html") && html.Contains("<body")) { return html.Remove(0, html.IndexOf("<body>") + 6).Replace("</body></html>", ""); } return html; }
Final output
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Word_files_preview387157141m
Note:
A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.
The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.
You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!