Articles in this section
Category / Section

How to Load and Upload Word Documents in JavaScript DocumentEditor?

4 mins read

How to upload and open Word documents from Azure file storage in Blazor – Document Editor

You can upload the content of the Document Editor as a Word document to Azure storage and load the Word documents in the Document Editor from Azure file storage.

Refer to the steps below where we will build the Blazor server-side application with the Document Editor component to programmatically connect with Azure file storage for opening and saving the Word documents.

Step 1: Please refer to the documentation below and integrate the Document Editor component in the Blazor server-side application.

https://blazor.syncfusion.com/documentation/document-editor/getting-started/server-side-application/?no-cache=1

Step 2: Install the Microsoft.Azure.Storage.File package for working with the Microsoft Azure Storage File service.

https://www.nuget.org/packages/Microsoft.Azure.Storage.File/11.1.5?_src=template

Step 3: Need a connection string to access data in an Azure Storage account at runtime. Refer to the link below for details on the connection string.

https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string

Index.razor

@page "/"
 
@using Syncfusion.Blazor.DocumentEditor
@using System.IO;
@using System.Net;
@using System.Web;
@using Newtonsoft.Json;
@using Microsoft.Azure.Storage;
@using Microsoft.Azure.Storage.File;
@using Syncfusion.Blazor.Buttons;
@using Newtonsoft.Json
<SfButton IsPrimary="true" @onclick="onOpen">Open</SfButton>
<SfButton IsPrimary="true" @onclick="onUpload">Upload</SfButton>
<SfDocumentEditorContainer ID="container" @ref="container" Height="590px" EnableToolbar="true">
    <DocumentEditorContainerEvents Created="OnLoad"></DocumentEditorContainerEvents>
</SfDocumentEditorContainer>
 
@code{
    SfDocumentEditorContainer container;

    // Set your connection string.

    // To check your connection string, please refer to the URL below.

    // https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string

    // Below is the dummy connection string. Set yours.

    string connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1";


    public void OnLoad(object args)

    {

        string filePath = "wwwroot/data/GiantPanda.docx";

        using (FileStream fileStream = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))

        {

            WordDocument document = WordDocument.Load(fileStream, ImportFormatType.Docx);

            string json = JsonConvert.SerializeObject(document);

            document.Dispose();

            fileStream.Dispose();

            DocumentEditorModule editor = container.GetDocumentEditor();

            editor.Open(json);

            editor.DocumentName = "GiantPanda.docx";

        }

    }


    public void onOpen()

    {

        // File share name

        // For creating file shares, select your storage account, then select File shares under File service, and then create a new file share by clicking on the File share button.

        // Set the newly created file share name to the share reference.

        string shareReference = "documents"; // Here we have created "documents" as the file share name. Please set yours.

        // File name to be loaded into Syncfusion Document Editor

        string fileReference = "GiantPanda.docx";

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);

        CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient();

        // Get file share

        CloudFileShare cloudFileShare = fileClient.GetShareReference(shareReference);

        if (cloudFileShare.Exists())

        {

            // Get the related directory

            CloudFileDirectory dir = cloudFileShare.GetRootDirectoryReference();

            if (dir.Exists())

            {

                // Get the file reference

                CloudFile file = dir.GetFileReference(fileReference);

                MemoryStream memoryStream = new MemoryStream();

                // Download file to local disk

                file.DownloadToStream(memoryStream);

                WordDocument document = WordDocument.Load(memoryStream, ImportFormatType.Docx);

                container.GetDocumentEditor().Open(JsonConvert.SerializeObject(document));

                document.Dispose();

                memoryStream.Dispose();

            }

        }

    }

    public async void onUpload()     {         DocumentEditorModule editor = container.GetDocumentEditor();         object base64Data = await editor.SaveAsBlob(FormatType.Docx);           Dictionary<string, string> documentContent = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(base64Data.ToString());         byte[] data = Convert.FromBase64String(documentContent["data"]);         Stream stream = new MemoryStream(data);           //Connect to Azure         CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);           // Create a reference to the file client.         CloudFileClient fileClient = storageAccount.CreateCloudFileClient();           // Get a reference to the file share we created previously.         CloudFileShare share = fileClient.GetShareReference("documents");           if (share.Exists())         {             // Generate a SAS for a file in the share             CloudFileDirectory rootDir = share.GetRootDirectoryReference();             CloudFile file1 = rootDir.GetFileReference("GiantPanda.docx");               Stream fileStream = stream;               file1.UploadFromStream(fileStream);             fileStream.Dispose();         }     } }      

 

Output

Sample output

In this sample, added a button was added to upload the Document Editor content as a Docx document to Azure file storage and a button to open the uploaded document in the Document Editor. You can download the sample from the link below.

https://www.syncfusion.com/downloads/support/directtrac/general/ze/DocEditorBlazorApp1688354291


Conclusion

I hope you enjoyed learning how to Load and Upload Word Documents in JavaScript DocumentEditor.

You can refer to our JavaScript DocumentEditor feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our JavaScript DocumentEditor example to understand how to create and manipulate data.

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied