1. Home
  2. Docs
  3. Llamachant Framework Modules
  4. Extended Functionality
  5. File Attachments

File Attachments

If you don’t want to store file attachment data in your database directly, we make the process of saving elsewhere easier. Use our FileAttachmentProcessorBase classes to store your data on the File System or in the cloud.

By implementing the IFileAttachment interface and using the correct FileAttachmentProcessorBase class, the process of storing attachment data elsewhere is easy.

				
					[FileAttachment(nameof(FileData))]
    public class Document : BaseObject, IFileAttachment
    {
        public Document(Session session) : base(session) { }

        public IFileData FileData { get => this; set { } }

        private int _Size;
        public int Size
        {
            get { return _Size; }
            set { SetPropertyValue<int>(nameof(Size), ref _Size, value); }
        }

        private string _StorageLocation;
        [Size(512), Browsable(false)]
        public string StorageLocation
        {
            get { return _StorageLocation; }
            set { SetPropertyValue<string>(nameof(StorageLocation), ref _StorageLocation, value); }
        }

        private string _FileName;
        public string FileName
        {
            get { return _FileName; }
            set { SetPropertyValue<string>(nameof(FileName), ref _FileName, value); }
        }

        public string CalculateStorageLocation()
        {
            //File Storage
            //Read this from a configuration file or a SingletonBO object
            return @"C:\Temp\TestAttachments";

            //Azure Blob Storage (Requires LlamachantFramework.FileAttachments.AzureBlobStorage Package)
            //    return "TestAttachments/MyFiles/";
        }

        public void Clear()
        {
            GetProcessor().Clear(this);
        }

        public void LoadFromStream(string fileName, Stream stream)
        {
            GetProcessor().LoadFromStream(fileName, stream, this);
        }

        public void SaveToStream(Stream stream)
        {
            GetProcessor().SaveToStream(stream, this);
        }

        private FileAttachmentProcessorBase GetProcessor()
        {
            //File System Storage
            return new FileStorageFileAttachmentProcessor() { DeleteOnClear = true, OverwriteExistingFiles = false };

            //Azure Blob Storage (Requires LlamachantFramework.FileAttachments.AzureBlobStorage Package)
            //string blobStorageCS = "DefaultEndpointsProtocol=https;AccountName=....;AccountKey=eu2V.....";
            //string containerName = "Documents";
            //return new AzureBlobStorageFileAttachmentProcessor(blobStorageCS, containerName) { DeleteOnClear = false, OverwriteExistingFiles = false, PublicAccessType = Azure.Storage.Blobs.Models.PublicAccessType.None };
        }
    }
				
			

Clean Up After Object Deleted

In some cases, you may want to delete your file data from storage after your business object is deleted. To do this, call the DeleteFromStorage() method after your object has been deleted.

				
					protected override void OnSaved()
{
    base.OnSaved();

    if (IsDeleted)
        GetProcessor().DeleteFromStorage(this);
}
				
			

Create a Custom FileAttachmentProcessorBase

If the provided FileAttachmentProcessorBase implementations don’t meet your needs, you can easily create your own.

				
					public abstract class FileAttachmentProcessorBase
    {
        public abstract void LoadFromStream(string fileName, Stream stream, IFileAttachment attachment);
        public abstract void SaveToStream(Stream stream, IFileAttachment attachment);
        public abstract void DeleteFromStorage(IFileAttachment attachment);
        public abstract void Clear(IFileAttachment attachment);

    }
				
			
Was this article helpful to you? Yes 3 No

How can we help?