Unable to determine file type from a stream. Please try to specify filename with extension explicitly

Hello, I was wondering if I could get some help with this error I am seeing.

We are using GroupDocs.Viewer for .NET version 16.10

My scenario is that I load a document from SharePoint and attempt to get the documentInfo from a stream of the document file bytes like so:

    ViewerHtmlHandler handler = new ViewerHtmlHandler(GetViewerConfig());

    using (MemoryStream ms = new MemoryStream(document.FileBytes))
    {
        **var documentInfo = handler.GetDocumentInfo(ms);**

        int maxWidth = 0;
        int maxHeight = 0;

        foreach (var page in documentInfo.Pages)
        {
            if (page.Height > maxHeight)
            {
                maxHeight = page.Height;
                maxWidth = page.Width;
            }
        }

        docInfo = new DocInfo()
        {
            DocumentGuid = documentInfo.Guid,
            DocumentName = document.FileName,
            PageCount = documentInfo.Pages.Count,
            PageNumber = 1,
            DocumentHeight = maxHeight,
            DocumentWidth = maxWidth
        };
    }

    return docInfo;

On the line marked in ** I get this exception
System.InvalidOperationException: ‘Unable to determine file type from a stream. Please try to specify filename with extension explicitly.’

Can anyone shed some light on this and provide some assistance?

Thanks

Just to add, I also tried GroupDocs.Viewer version 18.11 and I see the same error.

@danielmcgrath,

Thanks for contacting support.

Would you please also share with us the problematic documents for which you are facing this issue? We shall be looking forward to your response.

This is one of the emails (.MSG) from SharePoint that I am unable to get document info for from the MemoryStream.

Thanks

test CRM0002198.zip (35.3 KB)

@danielmcgrath,

Thanks for sharing the problematic document with us.

We are able to reproduce your reported exception at our end. This issue has been logged into our internal Issue Tracking System as VIEWERNET-1832 for further investigation. We shall keep you notified in case of any updates.

@danielmcgrath,

We have got the updates regarding your reported issue.

Although GroupDocs.Viewer tries to detect the file type of the input stream, in some cases the file type can’t be detected and the API throws the exception. Furthermore, the file type detection feature does not guarantee that file type would be detected correctly. So in your case, we recommend you to please use the method which accepts file name with extension as the second parameter:

var documentInfo = handler.GetDocumentInfo(ms, Guid.NewGuid() + ".msg");

(NOTE: Guid.NewGuid() is added to make sure that the API will create a separate directory in cache folder for each file.)

For reference, please download this (56.0 KB) sample application. Hope it helps.

@danielmcgrath,

The issue you have faced earlier (logged as VIEWERNET-1832) has been fixed in this release.

Hi @usman.aziz

I have just upgraded my GroupDocs.viewer to the latest version (19.5.0) for testing purposes.
I am finding that the viewer will not render any docs, so far I have tried Word, Excel, Text file, and an Email.
The error I get is that the file does not exist in my storage path.
This was working absolutely fine in the previous version I used (16.10.0).

Is this a known issue?

@danielmcgrath,

The file that you are rendering must be located in the folder that you have set as storage folder in the viewer’s configuration using ViewerConfig.StoragePath property. In case you haven’t set the storage folder, you can use the absolute path of the file. Please make sure that your source files exist in the storage folder that you have set in the configuration. In case the issue persists, please create and share with us a simple console application (source code without compilation error) that can be used to reproduce the issue at our end.

@usman.aziz

FYI I am getting the file to preview from sharepoint and getting the filebytes into a memory steam like so

        ViewerHtmlHandler handler = new ViewerHtmlHandler(GetViewerConfig());

        using (MemoryStream ms = new MemoryStream(document.FileBytes))
        {
            var documentInfo = handler.GetDocumentInfo(ms);

            int maxWidth = 0;
            int maxHeight = 0;

            foreach (var page in documentInfo.Pages)
            {
                if (page.Height > maxHeight)
                {
                    maxHeight = page.Height;
                    maxWidth = page.Width;
                }
            }

            docInfo = new DocInfo()
            {
                DocumentGuid = documentInfo.Guid,
                DocumentName = document.FileName,
                PageCount = documentInfo.Pages.Count,
                PageNumber = 1,
                DocumentHeight = maxHeight,
                DocumentWidth = maxWidth
            };
        }

It seems that in the old version handler.getDocumentInfo(ms) would somehow save the file to my storage location. However it appears that this does not happen in the latest version (19.5.0) which leads to me getting a file not found error.

@danielmcgrath,

Thanks for sharing the code.

Yes, you got it absolutely correct. The GetDocumentInfo(MemoryStream) method used to save the file in the storage folder in the older versions when ViewerConfig.UseCache is set to true. However, this functionality has been removed since version 17.6.0 and now the API directly uses the memory stream without saving it as a physical file.