logo

Creating a file from a stream

When working with external services or integrating with another system, you may need to create a file from a stream. There are many examples of saving files to the hard drive on the Internet, but in this case the file should be saved on the server.

Add the following assemblies:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using EleWise.ELMA.Files;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
 

Write a method:

private BinaryFile CreateBinaryFile(Stream stream, string fileName)
   {
         var temp = BinaryFile.CreateContentFilePath(fileName);
         using (var fs = new FileStream(temp, FileMode.CreateNew, FileAccess.Write))
         {
            stream.Seek(0, SeekOrigin.Begin);
            stream.CopyTo(fs);
         }
         var mimeMappingService = Locator.GetServiceNotNull<IMimeMappingService>();
         var contractTemplate = new BinaryFile
         {
            ContentType = mimeMappingService.GetTypeByExtension(Path.GetExtension(fileName)),
            Name = Path.GetFileName(fileName),
            ContentFilePath = temp,
            CreateDate = DateTime.Now,
         };
         DataAccessManager.FileManager.SaveFile(contractTemplate);
         return contractTemplate;
 

Now you can call it in this process.

Example of creating a document version:

var file = CreateBinaryFile(stream, name);
         PublicAPI.Docflow.DocumentVersion.AddDocumentVersion(context.Doc, file, DocumentVersionStatus.Current);