Download a DocumentDocuWare Platform .NET API
Get the stream and name of a document file

The suggested file name of the downloaded file is sent in the Content Disposition header. This example shows how the file name and other attributes, like the content type, is accessed. The asynchronous API must be used, because the synchronous API does provide access to the header fields. The current thread is blocked until the response arrives by using OnlineResult.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocuWare.Platform.ServerClient;

namespace DocuWare.PlatformClientExamples
{
    static partial class Examples
    {
        public class FileDownloadResult
        {
            public string ContentType { get; set; }
            public string FileName { get; set; }
            public long? ContentLength { get; set; }
            public System.IO.Stream Stream { get; set; }
        }

        static public FileDownloadResult DownloadDocumentContent(this Document document)
        {
            if (document.FileDownloadRelationLink == null)
                document = document.GetDocumentFromSelfRelation();

            var downloadResponse = document.PostToFileDownloadRelationForStreamAsync(
                new FileDownload()
                {
                    TargetFileType = FileDownloadType.Auto
                }).Result;

            var contentHeaders = downloadResponse.ContentHeaders;
            return new FileDownloadResult()
            {
                Stream = downloadResponse.Content,
                ContentLength = contentHeaders.ContentLength,
                ContentType = contentHeaders.ContentType.MediaType,
                FileName = contentHeaders.ContentDisposition.FileName
            };
        }
    }
}
Get the stream and name of a document file asynchronously

The header fields can be accessed straight forward in the asynchronous API.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocuWare.Platform.ServerClient;

namespace DocuWare.PlatformClientExamples
{
    static partial class ExamplesAsync
    {
        public class FileDownloadResult
        {
            public string ContentType { get; set; }
            public string FileName { get; set; }
            public long? ContentLength { get; set; }
            public System.IO.Stream Stream { get; set; }
        }

        static public async Task<FileDownloadResult> DownloadDocumentContentAsync(this Document document)
        {
            if (document.FileDownloadRelationLink == null)
                document = await document.GetDocumentFromSelfRelationAsync();

            var downloadResponse = await document.PostToFileDownloadRelationForStreamAsync(
                new FileDownload()
                {
                    TargetFileType = FileDownloadType.Auto
                });

            var contentHeaders = downloadResponse.ContentHeaders;
            return new FileDownloadResult()
            {
                Stream = downloadResponse.Content,
                ContentLength = contentHeaders.ContentLength,
                ContentType = contentHeaders.ContentType.MediaType,
                FileName = contentHeaders.ContentDisposition.FileName
            };
        }
    }
}
See Also

Other Resources