Edit a FileDocuWare Platform .NET API

If you want to edit a file, you must do the following steps:

  • Locate the section to be edited.
  • Download the content of the section into a temporary folder. Keep the content type information of the section.
  • Upload the file as new content of the section. You must specify the content type of the uploaded file.

This example shows the steps you have to implement in order to download and upload the files with the correct content type.

Edit a file asynchronously

In the following example it is assumed that the section to be edited is loacated already. How such the section is located depends on the client; typically a user selects the section in a browser or the client assumes that for a document always the first section is edited.

The example shows how to download the content of the file to a temporary folder. Then the section is uploaded again keeping the content type of the original content. Finally the temporary folder is deleted.

The step which edits the file is not part of the example. This depends on your client. For example, you could use a FileSystemWatcher to observe changes in the file.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DocuWare.Platform.ServerClient;
using DocuWare.Services.Http;

namespace DocuWare.PlatformClientExamples
{
    static partial class ExamplesAsync
    {
        public class FileReplacementData
        {
            public string Directory { get; set; }
            public string ContentType { get; set; }
            public string FilePath { get; set; }
            public Section Section { get; set; }

        }

        public async static Task<Section> EditFile(Section section)
        {
            var fileReplacementData = await GetFileForReplacementAsync(section);

            // Do something with the file, e.g. open it in external editor and wait until the editing operation is finished. 
            await Task.Delay(2000);

            return await ReplaceFileAsync(fileReplacementData);
        }


        public async static Task<FileReplacementData> GetFileForReplacementAsync(Section section)
        {
            using (DeserializedHttpResponse<System.IO.Stream> downloadResponse = await section.GetStreamFromFileDownloadRelationAsync())
            {
                var contentHeaders = downloadResponse.ContentHeaders;

                // Create a temporary directory 
                string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                Directory.CreateDirectory(tempDirectory);

                var fileName = contentHeaders.ContentDisposition.FileName.Replace("\"", "");
                var fileReplacementData = new FileReplacementData()
                {
                    FilePath = fileName,
                    ContentType = contentHeaders.ContentType.MediaType,
                    Directory = tempDirectory,
                    Section = section
                };

                using (var file = System.IO.File.Create(fileReplacementData.FilePath))
                using (var stream = downloadResponse.Content)
                    await stream.CopyToAsync(file);

                return fileReplacementData;
            }
        }

        public async static Task<Section> ReplaceFileAsync(FileReplacementData fileReplacementData)
        {
            try
            {
                using (var stream = File.OpenRead(fileReplacementData.FilePath))
                    return await fileReplacementData.Section.PutToContentRelationForSectionAsync(fileReplacementData.ContentType, stream);
            }
            finally
            {
                Directory.Delete(fileReplacementData.Directory, true);
            }
        }
    }
}