Download a Document
This example shows how you can download the file of a documents. In order to get access to the HTTP headers, which contain the suggested file name, the class DeserializedHttpResponse(T) is used.
Not that for the returned stream Close() or Dispose() must be called.
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 Result.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocuWare.Platform.ServerClient;
using DocuWare.Services.Http;
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; }
}
public static 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 = downloadResponse.GetFileName() //GetFileName() comes with DocuWare REST Client NuGet
};
}
}
}
Get the stream and name of a document file asynchronously
The header fields can be accessed straight forward in the asynchronous API.
C#
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; }
}
public static async Task<FileDownloadResult> DownloadDocumentContentAsync(this Document document)
{
if (document.FileDownloadRelationLink == null)
document = await document.GetDocumentFromSelfRelationAsync().ConfigureAwait(false);
var downloadResponse =
await document.PostToFileDownloadRelationForStreamAsync(
new FileDownload()
{
TargetFileType = FileDownloadType.Auto
})
.ConfigureAwait(false);
var contentHeaders = downloadResponse.ContentHeaders;
return new FileDownloadResult()
{
Stream = downloadResponse.Content,
ContentLength = contentHeaders.ContentLength,
ContentType = contentHeaders.ContentType.MediaType,
FileName = contentHeaders.ContentDisposition.FileName
};
}
}
}