Back to Devexpress

How to: Open a document using drag-and-drop

aspnet-119382-components-rich-text-editor-examples-how-to-open-a-document-using-drag-and-drop.md

latest7.1 KB
Original Source

How to: Open a document using drag-and-drop

  • Dec 17, 2020
  • 3 minutes to read

In some scenarios, it is necessary to load a document stored on a user’s hard disk to the ASPxRichEdit control. The ASPxRichEdit does not support this feature, but you can use a workaround described in this example. Note, that in this example the open document is not saved to the Working Directory. The ASPxUploadControl is developed to save client files to the server via a browser. This control includes the Advanced mode that allows users to load documents using drag-and-drop. The ASPxUploadControl’s ExternalDropZoneID property defines which HTML element is used to release files dragged from the client’s hardware. Using this property, you can invoke document loading by dragging it to the ASPxRichEdit control. When the ASPxUploadControl finishes loading the document, it is saved as a stream via the RichEditDocumentServer and opened in the ASPxRichEdit control.

View Example

csharp
using DevExpress.Web.Office;
using DevExpress.XtraRichEdit;
using System.IO;

public partial class _Default : System.Web.UI.Page {
    string uploadedDocId = "uploadedDocId";

    protected void UploadControl_FileUploadComplete(object sender, DevExpress.Web.FileUploadCompleteEventArgs e) {
        if(e.UploadedFile.IsValid)
            e.CallbackData = e.UploadedFile.FileNameInStorage;
    }

    protected void RichEdit_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e) {
        string path = MapPath(UploadControl.FileSystemSettings.UploadFolder) + "\\" + e.Parameter;
        using(MemoryStream stream = new MemoryStream()) {
            using(RichEditDocumentServer server = new RichEditDocumentServer()) {
                server.LoadDocument(path);
                server.SaveDocument(stream, DocumentFormat.OpenXml);
                stream.Position = 0;
                DocumentManager.CloseDocument(uploadedDocId);
                RichEdit.Open(uploadedDocId, DocumentFormat.OpenXml, () => stream);
            }
        }
        File.Delete(path);
    }
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxRichEdit.v16.1, Version=16.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxRichEdit" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.v16.1, Version=16.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onUploadControlFileUploadComplete(s, e) {
            if(e.isValid)
                RichEdit.PerformCallback(e.callbackData);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxRichEdit ID="RichEdit" runat="server" OnCallback="RichEdit_Callback">
        </dx:ASPxRichEdit>

        <dx:ASPxUploadControl ID="UploadControl" runat="server" UploadMode="Auto" AutoStartUpload="True" ClientVisible="false" UploadStorage="FileSystem"
            OnFileUploadComplete="UploadControl_FileUploadComplete">
            <AdvancedModeSettings EnableDragAndDrop="True" EnableFileList="False" EnableMultiSelect="False" ExternalDropZoneID="RichEdit" />
            <FileSystemSettings UploadFolder="~\App_Data\Uploads" />
            <ClientSideEvents FileUploadComplete="onUploadControlFileUploadComplete" />
            <ValidationSettings AllowedFileExtensions=".doc,.docx,.rtf,.txt" MaxFileSize="4194304" />
        </dx:ASPxUploadControl>
    </div>
    </form>
</body>
</html>
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxRichEdit.v16.1, Version=16.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxRichEdit" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.v16.1, Version=16.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onUploadControlFileUploadComplete(s, e) {
            if(e.isValid)
                RichEdit.PerformCallback(e.callbackData);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxRichEdit ID="RichEdit" runat="server" OnCallback="RichEdit_Callback">
        </dx:ASPxRichEdit>

        <dx:ASPxUploadControl ID="UploadControl" runat="server" UploadMode="Auto" AutoStartUpload="True" ClientVisible="false" UploadStorage="FileSystem"
            OnFileUploadComplete="UploadControl_FileUploadComplete">
            <AdvancedModeSettings EnableDragAndDrop="True" EnableFileList="False" EnableMultiSelect="False" ExternalDropZoneID="RichEdit" />
            <FileSystemSettings UploadFolder="~\App_Data\Uploads" />
            <ClientSideEvents FileUploadComplete="onUploadControlFileUploadComplete" />
            <ValidationSettings AllowedFileExtensions=".doc,.docx,.rtf,.txt" MaxFileSize="4194304" />
        </dx:ASPxUploadControl>
    </div>
    </form>
</body>
</html>
vb
Imports DevExpress.Web.Office
Imports DevExpress.XtraRichEdit
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Private uploadedDocId As String = "uploadedDocId"

    Protected Sub UploadControl_FileUploadComplete(ByVal sender As Object, ByVal e As DevExpress.Web.FileUploadCompleteEventArgs)
        If e.UploadedFile.IsValid Then
            e.CallbackData = e.UploadedFile.FileNameInStorage
        End If
    End Sub

    Protected Sub RichEdit_Callback(ByVal sender As Object, ByVal e As DevExpress.Web.CallbackEventArgsBase)
        Dim path As String = MapPath(UploadControl.FileSystemSettings.UploadFolder) & "\" & e.Parameter
        Using stream As New MemoryStream()
            Using server As New RichEditDocumentServer()
                server.LoadDocument(path)
                server.SaveDocument(stream, DocumentFormat.OpenXml)
                stream.Position = 0
                DocumentManager.CloseDocument(uploadedDocId)
                RichEdit.Open(uploadedDocId, DocumentFormat.OpenXml, Function() stream)
            End Using
        End Using
        File.Delete(path)
    End Sub
End Class