DocuVieware Lite unable to open text (.txt) files via stream

Discussions about DocuVieware integration in GdPicture.NET.
Post Reply
stagger
Posts: 6
Joined: Tue Mar 05, 2019 5:42 am

DocuVieware Lite unable to open text (.txt) files via stream

Post by stagger » Fri Mar 08, 2019 3:22 am

Hi,

I'm trying to load file contents in DocuVieware Lite programatically using a REST service, using the following code:

Code: Select all

    DocuVieware docuViewareInstance = new DocuVieware(sessionId);

    using (StringWriter controlOutput = new StringWriter())
    {
        if (!string.IsNullOrWhiteSpace(filePath))
        {
            var stream = this.GetDocumentStream(parameters.FilePath);

            docuViewareInstance.LoadFromStream(stream, true);
        }

        docuViewareInstance.RenderControl(controlOutput);

        return new DocuViewareRESTOutputResponse
        {
            HtmlContent = controlOutput.ToString()
        };
    }
This code works as expected when loading .docx, .pdf, and various image file formats, but will not load .txt files at all. The online sample project loads the same file I'm trying to load, but I just get a blank control when a text file is selected (similar to when an unsupported format like .xls is selected).

Any help would be greatly appreciated.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by Gabriela » Fri Mar 08, 2019 4:30 pm

Hi,

It is not quite clear what "var stream = this.GetDocumentStream(parameters.FilePath);" does.
As you say, your txt file opens just fine in the online demo, so the issue is probably somewhere outside of DocuVieware Lite.

stagger
Posts: 6
Joined: Tue Mar 05, 2019 5:42 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by stagger » Mon Mar 11, 2019 11:09 pm

That is a simple private function which opens a read-only stream for the specified file:

Code: Select all

    private Stream GetDocumentStream(string filePath)
    {
        return new FileStream(filePath, FileMode.Open, FileAccess.Read);
    }
I can debug through the generation ok, and can see the stream being created. As mentioned, this code works for other file types without issue, so I can't see why it wouldn't work for .txt files, so if there is anything else I can try, please let me know.

Thanks.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by Gabriela » Thu Mar 14, 2019 5:15 pm

Hello,

Did you try to use this method for text files?
https://www.docuvieware.com/guides/aspn ... ring).html
The DocumentName parameter makes a difference here because text files do not have a header identifying their format. Your code looks correct, this is the only difference what I see when comparing the demo projects with your code, as you say "The online sample project loads the same file I'm trying to load.".

stagger
Posts: 6
Joined: Tue Mar 05, 2019 5:42 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by stagger » Mon Mar 18, 2019 12:54 am

That worked.

Thank you for your help.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by Gabriela » Mon Mar 18, 2019 8:44 am

Hi,

I'm glad to hear that. Feel free to ask if you will need further assistance.

stagger
Posts: 6
Joined: Tue Mar 05, 2019 5:42 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by stagger » Wed Mar 20, 2019 5:01 am

One further question. When I use wrap the stream in a using statement, all file types load, except PDFs. Is this expected behaviour?

e.g., this code works for all tested file types:

Code: Select all

    var stream = this.GetDocumentStream(parameters.FilePath);

    docuViewareInstance.LoadFromStream(stream, true);
However, the following does not work for PDF files:

Code: Select all

    using (var stream = this.GetDocumentStream(parameters.FilePath))
    {
        docuViewareInstance.LoadFromStream(stream, true);
    }
Is this something to do with how PDF files are loaded?

I should point out that I receive a valid response from the REST service in all cases, but for PDF files, a blank DocuVieware Lite control is rendered on the page (i.e., it shows the header bar, but no content).

Many thanks.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: DocuVieware Lite unable to open text (.txt) files via stream

Post by Gabriela » Wed Mar 20, 2019 11:24 am

Hi,

Please pay attention to the OwnStream parameter here. If it is set to true, the control is in charge of disposing of the input stream. But you can't really know for sure when the control will do it as it may be asynchronous. So that could be the reason for not displaying the document. Another way is to mark OwnStream as false and handle the stream disposal on your own. That's usually what is recommended.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest