Back to Devexpress

File Attachments (Store Custom Files)

expressappframework-112781-document-management-file-attachments-module.md

latest12.6 KB
Original Source

File Attachments (Store Custom Files)

  • Feb 16, 2026
  • 7 minutes to read

XAF includes a File Attachments module and file data types for file management (upload, download, open, and save files). This module contains Property Editors and Controllers for the file data type.

Run Demo: File Attachment Properties ASP.NET Core Blazor

Watch Video: XAF - Store file attachments in Dropbox instead of the database (XPO)

Supported Functionality

ASP NET Core Blazor

DxFileDataPropertyEditor displays the property’s value as a hyperlinked file name. A user can click the link to download the file. The Detail View displays the Select File and Clear buttons. A user should save changes after a file upload to display a link instead of a plain text file name. The editor displays upload progress.

The default maximum file size is 4 MB.

FileAttachmentsBlazorModule uses the DxFileInput underlying component. Access it to customize file uploading options (for example, maximum file size or accepted file type).

File: MySolution.Blazor.Server\Controllers\CustomizeFileSizeController.cs

csharp
using DevExpress.ExpressApp.FileAttachments.Blazor.Editors;
using DevExpress.ExpressApp;
using MySolution.Module.BusinessObjects;

namespace MySolution.Blazor.Server.Controllers;
public class CustomizeFileSizeController : ObjectViewController<DetailView, Resume> {
    protected override void OnActivated() {
        View.CustomizeViewItemControl<DxFileDataPropertyEditor>(this, editor => {
            editor.ComponentModel.AcceptedFileTypes = [".jpg", ".gif"];
            editor.ComponentModel.MaxFileSize = 1024 * 512;
        });
    }
}

Alternatively, use the FileAttachmentsOptions.DefaultMaxFileSize property to change the default maximum size:

File : MySolution.Blazor.Server\Startup.cs.

csharp
using DevExpress.ExpressApp.ApplicationBuilder;
using DevExpress.ExpressApp.Blazor.ApplicationBuilder;
using DevExpress.Persistent.BaseImpl;
// ...
public class Startup {
   // ...
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddXaf(Configuration, builder => {
            builder.UseApplication<MySolutionBlazorApplication>();
            builder.Modules
                // ...
                .AddFileAttachments(options => {
                    options.DefaultMaxFileSize = 2097152;
                });
            // ...
        });
        // ...
    }
}

Windows Forms

Attach a FileWhen a user clicks the ellipsis button on the FileDataPropertyEditor, the application invokes the OpenFileDialog dialog, which you can use to select an attached file.Save an Attached File to a DiskThe FileDataPropertyEditor context menu contains the SaveTo Action. The FileAttachmentController.SaveFileData method handles this Action’s Execute event. You can override this method in the FileAttachmentController descendant to change the default logic.Open an Attached FileThe FileDataPropertyEditor context menu contains the Open Action. The FileAttachmentController.Open method handles this Action’s Execute event. You can override this method in the FileAttachmentController descendant to change the default logic.Detach a FileThe FileDataPropertyEditor context menu contains the ClearContent Action. This Action’s Execute event handler calls the property type’s Clear method to clear file content. Override the IFileData.Clear method to implement your logic.

File Attachments Module Components

The following table contains classes for different platforms:

|

Platform

|

Module Class

|

NuGet package

| | --- | --- | --- | |

ASP.NET Core Blazor

|

FileAttachmentsBlazorModule

|

DevExpress.ExpressApp.FileAttachment.Blazor

| |

WinForms

|

FileAttachmentsWindowsFormsModule

|

DevExpress.ExpressApp.FileAttachment.Win

|

The File Attachments Module contains the following Property Editors to display file data properties in the UI:

  • DevExpress.ExpressApp.FileAttachment.Blazor.FileDataPropertyEditor
  • DevExpress.ExpressApp.FileAttachment.Win.FileDataPropertyEditor

Add the File Attachments Module to an XAF Application

Install the appropriate platform-specific NuGet package and use one of the following techniques to add the File Attachments Module:

Entity Framework Core-Based Application

The following additional step is required if you use Entity Framework Core:

  1. Navigate to the YourSolutionName.Module\BusinessObjects\YourSolutionNameDbContext.cs file and include the FileData entity in the data model:

Define a File Data Object and Storage

The file data object is a business class that implements the IFileData interface. You can also use the built-in FileData class (XPO: the %PROGRAMFILES%\DevExpress 25.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl\FileData.cs file, EF Core: the %PROGRAMFILES%\DevExpress 25.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.EF\FileData.cs file).

csharp
using DevExpress.Persistent.Base;

namespace MySolution.Module.BusinessObjects {
    [FileAttachmentAttribute(nameof(File))]
    public class MyFileAttachment : BaseObject {
        // ...
        [ExpandObjectMembers(ExpandObjectMembers.Never)]
        [FileTypeFilter("DocumentFiles", 1, "*.txt", "*.doc")]
        [FileTypeFilter("AllFiles", 2, "*.*")]
        public virtual FileData File { get; set; }
    }
    public class FileData : BaseObject, IFileData {
        // ...
    }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
csharp
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;

namespace MySolution.Module.BusinessObjects {
    [FileAttachmentAttribute(nameof(File))]
    public class MyFileAttachment : BaseObject {
        // ...
        private FileData file;
        [Aggregated, ExpandObjectMembers(ExpandObjectMembers.Never)]
        [FileTypeFilter("DocumentFiles", 1, "*.txt", "*.doc")]
        [FileTypeFilter("AllFiles", 2, "*.*")]
        public FileData File {
            get { return file; }
            set { SetPropertyValue(nameof(File), ref file, value); }
        }
    }
    public class FileData : BaseObject, IFileData {
    // ...
    }
}

Refer to the following help topic for more information on file data properties: File Attachment Properties.

XAF stores attached files in a database in a binary form. When you use the DevExpress.Persistent.BaseImpl.FileData type, the gzip compression is applied to a file. Maximum file size is 2 GB.

Upload and Download File Attachments Programmatically (in Code)

Call the IFileData.LoadFromStream method to upload a file in code. To obtain the file, call the IFileData.SaveToStream method. The LoadFromStream method does not require a full path to the file. This method accepts a file name as the first parameter.

Examples

View Example: How to: Store file attachments in the file system instead of the databaseView Example: How to: Store file attachments in Dropbox instead of the databaseView Example: How to: Use the File Attachment Module with a legacy database

Tip

The following example implements a business class with a file data property and a file collection property: How to: Implement File Data Properties.

Read Compressed Files in the FileData Database Table from External Non-XAF .NET Applications

XAF stores attached files in a database in a binary form. Maximum file size is 2 GB. The XPO DevExpress.Persistent.BaseImpl.FileData type compresses files when it adds them to the database and decompresses files when they are accessed in the database. The DevExpress.Persistent.Base.CompressionConverter class (a custom XPO Value Converter class from the DevExpress.Persistent.BaseImpl.Xpo assembly) applies GZIP compression to files. For more information on the GZIP compression algorithm, refer to the source code at %PROGRAMFILES%\DevExpress 25.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.Xpo\CompressionUtils.cs.

Use one of the following options to read compressed files in the FileData database table from external non-XAF .NET apps:

  • If you can use XPO and FileData for data access, call the SaveToStream(Stream) method.

  • Otherwise, call the CompressionConverter.ConvertFromStorageType method as shown below:

  • If you do not need default file compression, you can create a custom IFileData implementation and use it instead of the DevExpress.Persistent.BaseImpl.FileData type in your application. To do this, copy the source code of the FileData.cs class. You can find it in the %PROGRAMFILES%\DevExpress 25.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.Xpo folder. In the copied code, remove the [ValueConverter(typeof(CompressionConverter))] line from the Content property, and rename the class and namespaces.

See Also

File Attachment Properties

Built-in Business Classes & Interfaces

Implement File Data Properties

Attach Files to Objects (.NET)

LoadFromStream(String, Stream)

SaveToStream(Stream)