What is best way to implement GroupDocs Viewer for .NET MVC C#?

Hi Team,

We are using an Azure Storage account for file storage and we have millions of files.
If Customer wants to see the files, they login into our application and click the file to view.
Our application will download the file as a Byte array and load in our application.

But now we are trying to view the RTF, excel, word and other browsers not supported files using GroupDocs viewer.

For this what is the best way to implement the GroupDocs viewer without storing the file local path with viewer control integration with our Application?

@mohantriyam

I’m sorry for the delayed response. Can you please clarify if you’re going to use GroupDocs.Viewer.UI package or GroupDocs.Viewer for .NET directly?

Hi @vladimir.litvinchik

We have different types of file formats.
We are going to integrate the UI package with our product.
but for now we are planning GroupDocs.Viewer for .NET directly in our MVC application.

Can you please suggest what is the best option for us?
Please provide a few differences for GroupDocs.Viewer.UI & GroupDocs.Viewer for .NET.

@mohantriyam

GroupDocs.Viewer for .NET is high code .NET library that enables you to convert one of the supported file formats in HTML/PNG/JPEG/PDF. There is no UI included in this package.

GroupDocs.Viewer.UI is Angular UI for GroupDocs.Viewer for .NET that you can integrate in your ASP.NET Core Web Application. You can learn more about GroupDocs.Viewer UI at the repository page GitHub - groupdocs-viewer/GroupDocs.Viewer-for-.NET-UI: UI - User Interface for GroupDocs.Viewer for .NET document viewer and automation API..

In case you’re planning to integrate GroupDocs.Viewer for .NET in ASP.NET MVC (.NET Framework) application please check our MVC Demo.

Hi @vladimir.litvinchik

Thanks for the information.
I saw the sample code on the github site. All files are stored in the local server path. It will occupy my server storage.
So Is it possible to convert to the Memory Stream concept using GroupDocs.Viewer for .NET?

@mohantriyam

In general, it doesn’t matter where the data is stored. You can attach any storage, for example you can use in-memory cache. Can you please clarify if you want to store the source file in-memory without storing it to the local disk?

@vladimir.litvinchik

Can you please clarify if you want to store the source file in-memory without storing it to the local disk? —> Yes, without storing it in local disk.

Actually we have files in our Azure Storage account.

In C#, we download the file and store it as a Byte Array(Source file as RTF, word, excel, etc…,).

Now our customer wants to view the above file formats in the browsers.

So we are trying to use GroupDocs Viewer. In this case I saw a few examples in github, the output file is storing in the local server path.

Is that possible to store output file in Memory Stream?

@mohantriyam

I’m sorry for the delayed response.

Yes, it is possible to store the source files and output files in memory. Please find the sample-app.zip (4.5 KB) that demonstrates how you can implement custom file storage that is responsible for reading a file from any source without storing it to the local disk. Here is a complete listing of the Startup.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GroupDocs.Viewer.UI.Core;
using GroupDocs.Viewer.UI.Core.Entities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace GroupDocs.Viewer.AspNetCore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IFileStorage, MyFileStorage>();

            services
                .AddGroupDocsViewerUI();

            services
                .AddControllers()
                .AddGroupDocsViewerSelfHostApi()
                .AddInMemoryCache(config => config
                    .SetGroupCacheEntriesByFile(true)
                    .SetCacheEntryExpirationTimeoutMinutes(5)
                );
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app
                .UseRouting()
                .UseEndpoints(endpoints =>
                {
                    endpoints.MapGroupDocsViewerUI(options =>
                    {
                        options.UIPath = "/viewer";
                        options.APIEndpoint = "/viewer-api";
                    });
    
                    endpoints.MapGroupDocsViewerApi(options =>
                    {
                        options.ApiPath = "/viewer-api";
                    });
                });
        }
    }

    public class MyFileStorage : IFileStorage
    {
        public Task<IEnumerable<FileSystemEntry>> ListDirsAndFilesAsync(string dirPath)
        {
            var entries = new List<FileSystemEntry> { 
                 FileSystemEntry.File("sample.txt", "sample.txt", 123)
            };

            return Task.FromResult<IEnumerable<FileSystemEntry>>(entries);
        }

        public Task<string> WriteFileAsync(string fileName, byte[] bytes, bool rewrite)
        {
            throw new NotImplementedException("Implement this method to support file upload.");
        }

        public Task<byte[]> ReadFileAsync(string filePath)
        {
            //TODO: read your file here and return bytes

            var bytes = Encoding.UTF8
                .GetBytes("File path: " + filePath); 

            return Task.FromResult(bytes);
        }
    }
}

Run the application with dotnet run and navigate to https://localhost:9091/viewer in your browser. Browse the files using Folder icon and select a single sample.txt file. The file will be rendered in memory and never stored on the local disk. When the rendering will be finished the following page should appear

Please let us know if it works for you.

Hi @vladimir.litvinchik

Thanks for giving the sample code. We will try this code.
In the mean time I have one more question.
Now we are planning to integrate GroupDocs UI with our application.

Is it possible to integrate with our application.?

Our Application details are below:

.NET Framework Version is: 4.8
MVC Application
JS Framework: JQuery, Knockout JS for 2 way binding
Bootstrap & CSS for Design

@mohantriyam

GroupDocs.Viewer.UI package and related packages are designed to be used in ASP.NET MVC Core Web applications, so it won’t work with ASP.NET MVC. We do have plans to ship packages for ASP.NET MVC in the future but for now, you can give a try our MVC Demo. In MVC Demo there is no concept of FileStorage so there would be more places that should be updated to support in-memory storage.

Hi @vladimir.litvinchik,

Thank you,
Then we will try to use the GroupDocs.Viewer .

@mohantriyam

You’re welcome! Feel free to ask any questions.