expressappframework-112781-document-management-file-attachments-module.md
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)
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
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.
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;
});
// ...
});
// ...
}
}
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.
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.FileDataPropertyEditorDevExpress.ExpressApp.FileAttachment.Win.FileDataPropertyEditorInstall the appropriate platform-specific NuGet package and use one of the following techniques to add the File Attachments Module:
You can add modules to your application when you use the Template Kit to create a new XAF solution. Select modules in the Additional Modules section.
In XAF applications, you can call the AddFileAttachments(IModuleBuilder<IWinApplicationBuilder>, Action<FileAttachmentsOptions>) method in your ASP.NET Core Blazor/WinForms application builder.
Alternatively, you can add these Modules to the ModuleBase.RequiredModuleTypes collection of the platform-specific Module.
The following additional step is required if you use Entity Framework Core:
FileData entity in the data model: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).
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.
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.
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.
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.
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
Built-in Business Classes & Interfaces
Implement File Data Properties
Attach Files to Objects (.NET)