We're sorry GroupDocs doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.com

Decode a Base64 Stream and Display

Hello,

I am trying to view Base64 stream files. Is there any program that can decode, detect the file type and view the files ?

@danishPersys

Please note there is no direct option to render base64 string to HTML/Image/PDF in GroupDocs.Viewer Cloud. Currently, it takes input documents from cloud storage and puts output HTML/Image/PDF there.

Please confirm you are interested in GroupDocs.Viewer Cloud API or GroupDocs.Viewer on-premise API. As in on-premise API, you can convert Base64 string to byte array and pass it to Viewer object for rendering.

Thank you for the response. I am interested in on-premise API but I cannot find the Viewer class from your github repositories. May I know how do I make the viewer pops out using viewer class?

@danishPersys

Please check this sample-app.zip (3.3 KB) that is based on https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET-UI. Unpack and start the app by typing dotnet run, navigate to https://localhost:7104/viewer in your browser to open the viewer.
All the logic related to decoding from base 64 you can find in the Program.cs file

using GroupDocs.Viewer;
using GroupDocs.Viewer.UI.Core;
using GroupDocs.Viewer.UI.Core.Entities;
using GroupDocs.Viewer.UI.SelfHost.Api;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<IFileStorage, MyFileStorage>();
builder.Services.AddTransient<IFileTypeResolver, MyFileTypeResolver>();

builder.Services
        .AddGroupDocsViewerUI();

builder.Services
        .AddControllers()
        .AddGroupDocsViewerSelfHostApi(config =>
        {
            //Trial limitations https://docs.groupdocs.com/viewer/net/evaluation-limitations-and-licensing-of-groupdocs-viewer/
            //Temporary license can be requested at https://purchase.groupdocs.com/temporary-license
            //config.SetLicensePath("c:\\licenses\\GroupDocs.Viewer.lic"); // or set environment variable 'GROUPDOCS_LIC_PATH'
        })
        .AddLocalCache("./Cache");

var app = builder.Build();

app
    .UseRouting()
    .UseEndpoints(endpoints =>
    {
        endpoints.MapGroupDocsViewerUI(options =>
        {
            options.UIPath = "/viewer";
            options.APIEndpoint = "/viewer-api";
        });
        endpoints.MapGroupDocsViewerApi(options =>
        {
            options.ApiPath = "/viewer-api";
        });
    });

app.Run();

class MyFileStorage : IFileStorage
{
    public Task<IEnumerable<FileSystemEntry>> ListDirsAndFilesAsync(string dirPath)
    {
        var files = new List<FileSystemEntry>
        { 
            FileSystemEntry.File("my-base-64-file", "my-base-64-file", 0)
        };

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

    public Task<byte[]> ReadFileAsync(string filePath)
    {
        //TODO: retrieve the file by filePath 
        var base64 = "iVBORw0KGgoAAAA...[TRUNCATED]";
        var bytes = Convert.FromBase64String(base64);

        return Task.FromResult<byte[]>(bytes);
    }

    public Task<string> WriteFileAsync(string fileName, byte[] bytes, bool rewrite)
    {
        throw new NotImplementedException();
    }
}


class MyFileTypeResolver : IFileTypeResolver
{
    public Task<FileType> ResolveFileTypeAsync(string filePath)
    {
        return Task.FromResult<FileType>(FileType.PNG);
    }
}

As you can see in the code snippet above I’ve implemented the custom storage MyFileStorage that converts base 64 string into byte array and padding it to the viewer. In case you already know what file type you are processing you can directly pass it or try to detect the file type by using FileType.FromStream() method.

We can detect the type of a file by contents, see the complete list of supported file types at https://docs.groupdocs.com/viewer/net/supported-document-formats/. The file types that we can detect are marked with a tick.

1 Like