Error:GroupDocs.Viewer.Exceptions.GroupDocsViewerException: Failed to detect file type

We are using GroupDocs.Viewer Library with Dot net core 6 .We are trying to view the document through stream .The formats we are using are .rtf,docs,pdf .While reterviewing the file in groupdocs viewer we are getting error below is a sample error trace

GroupDocs.Viewer.Exceptions.GroupDocsViewerException: Failed to detect file type.
at GroupDocs.Viewer.Viewer.1
at GroupDocs.Viewer.Viewer.1
at GroupDocs.Viewer.Viewer.[1](BaseViewOptions [1], CancellationToken


)
at [1].
[1](Func`3 [1], ViewOptions


, CancellationToken )

@VikashVerma551

Can you please attach a couple of files that you’re trying to process? In case you know the type you can pass it to workaround this issue, see Specify file type when loading a document.

Hi I am using .rtf file .
I can’t attach the file as its confidential

@using (Viewer viewer = new Viewer(Model.contentDto.Content))
{
    HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(Model.contentDto.Name);
    viewer.View(options);
}

in content i have stream

public Stream? Content { get; set; }

@VikashVerma551

Please try adding additional parameter to the Viewer constructor

LoadOptions loadOptions = new LoadOptions(FileType.RTF);

using (Viewer viewer = new Viewer(Model.contentDto.Content, loadOptions))
{
...

Also, please make sure the stream pointer Model.contentDto.Content is at the beginning by adding Model.contentDto.Content.Position = 0.

Hi,

Its still not working. But let me tell you what i am trying to do.

I have documents like .doc,.docx,.rtf,pdf and other types also. i am getting this as a stream from azure blob and i want to return data from dot core web api to my react client so that at react client only user can see the docs .User should not be able to download the document.

 LoadOptions loadOptions = new LoadOptions(FileType.RTF);
    Model.contentDto.Content.Position = 0;
    using (Viewer viewer = new Viewer(Model.contentDto.Content, loadOptions))
    {

        HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(Model.contentDto.Name);
        viewer.View(options);
    }

error i am getting is System.NotSupportedException: Specified method is not supported.

Hi ,

I have solved the issue. Just want to know how to return this viewer to client. Any idea?

@VikashVerma551

The code you’re running is producing HTML files, and you can return the output to a client. In case you need a complete client-side solution please check GitHub - groupdocs-viewer/GroupDocs.Viewer-for-.NET-UI: UI - User Interface for GroupDocs.Viewer for .NET document viewer and automation API..

It’s creating multiple html files for a single document!.any way out it generate only 1 html file so that i can send the same in response

@VikashVerma551

Since you’re processing RTF files you can render them to one page, set options.RenderToSinglePage. See Render text documents as HTML, PDF, and image files | Documentation for more details.

Can we pass other files also in LoadOptions
like i want to open RTF,Docx and other .But i am not aware what file will at what time

Can we pass other files also in LoadOptions
like i want to open RTF,Docx and other .But i am not aware what file will come at what time from backend

@VikashVerma551

In case the file stream is not at the beginning Viewer may fail to detect the file type. Try removing loadOptions as a second parameter. Alternatively, you can try detecting the file type before rendering using FileType.FromStream() method so you can return a nice error message when Viewer fails to detect the file type.

Model.contentDto.Content.Position = 0;
FileType fileType = FileType.FromStream(Model.contentDto.Content);
if(fileType == FileType.Unknown)
{ 
     //TODO: return error message else proceed
}

LoadOptions loadOptions = new LoadOptions(fileType);
using (Viewer viewer = new Viewer(Model.contentDto.Content, loadOptions))
{
    HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(Model.contentDto.Name);
    viewer.View(options);
}

@VikashVerma551

Have you got a chance to try the code above?

Hi ,

IT was working but now the issue is if i am getting an html file it’s failing and giving file type as UNknown.

@VikashVerma551

Can you please share the files you’re trying to render so we could take a look. Also please share the OS, Framework (.NET, .NET Core, or .NET Framework) version and GroupDocs.Viewer for .NET version.

Hi @vladimir.litvinchik,

Please help us with below code.

 string returnContent = string.Empty;
 inputFile.Position = 0;
 FileType fileType = FileType.FromExtension(fileExtension);
 LoadOptions loadOptions = new LoadOptions(fileType);
 List<MemoryStream> pages = new List<MemoryStream>();
 using (Viewer viewer = new Viewer(inputFile, loadOptions))
{
     MemoryPageStreamFactory pageStreamFactory = new MemoryPageStreamFactory(pages);
     HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageStreamFactory);
     //to get all data in one file
     options.RenderToSinglePage = true;
     viewer.View(options);
}      
return returnContent;

I want to read the stream that we have in pageStreamFactory . Can you help us how we can do so.

Thanks,

@VikashVerma551

I’ve moved your question to this topic - How to get HTML when using MemoryPageStreamFactory.

Thanks @vladimir.litvinchik it worked

@VikashVerma551

You’re welcome!