Edit a File
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.
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.
C#
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 static async Task<Section> EditFile(Section section)
{
var fileReplacementData = await GetFileForReplacementAsync(section).ConfigureAwait(false);
// Do something with the file, e.g. open it in external editor and wait until the editing operation is finished.
await Task.Delay(2000).ConfigureAwait(false);
return await ReplaceFileAsync(fileReplacementData).ConfigureAwait(false);
}
public static async Task<FileReplacementData> GetFileForReplacementAsync(Section section)
{
using (var downloadResponse = await section.GetStreamFromFileDownloadRelationAsync().ConfigureAwait(false))
{
var contentHeaders = downloadResponse.ContentHeaders;
// Create a temporary directory
var 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).ConfigureAwait(false);
return fileReplacementData;
}
}
public static async Task<Section> ReplaceFileAsync(FileReplacementData fileReplacementData)
{
try
{
using (var stream = File.OpenRead(fileReplacementData.FilePath))
return
await fileReplacementData
.Section
.PutToContentRelationForSectionAsync(fileReplacementData.ContentType, stream)
.ConfigureAwait(false);
}
finally
{
Directory.Delete(fileReplacementData.Directory, true);
}
}
}
}