File Attachment Properties
- 3 minutes to read
Add the File Attachments Module to your application to use the file attachment functionality. The following lessons contain a detailed explanation on how to use the File Attachments Module:
- Attach files to objects in WinForms and ASP.NET Core Blazor applications.
XAF displays File Data Property Editors for properties that hold file data. Such properties must use a type that implements the IFileData interface. For example, you can use XAF’s built-in FileData
type.
Note
You can find an example of the File Attachment property editor in the Property Editors | File Attachment Properties section of the Feature Center demo installed with XAF. The default location of the application is %PUBLIC%\Documents\DevExpress Demos 24.2\Components\XAF\FeatureCenter.NETFramework.XPO.
Examples
ASP.NET Core Blazor
DxFileDataPropertyEditor
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.
Customize Maximum File Size and Allowed File Types
The following code snippet changes the uploaded file’s maximum size and specifies the allowed file types.
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;
});
}
}
WinForms
Each WinForms Property Editor has a control that displays the corresponding property in a Detail View, and a repository item that displays the property in a List Editor and supports in-place editing.
FileDataPropertyEditor
- Control
- FileDataEdit - a ButtonEdit descendant. The editor uses OpenFileDialog to attach a file.
- Repository Item
- RepositoryItemFileDataEdit - a RepositoryItemButtonEdit descendant.
- Description
- The editor allows a user to perform the Open, SaveTo, and ClearContent Actions that FileAttachmentController contains. Use the editor’s context menu to execute these Actions. You can also click the editor’s button or press Enter to execute the Open Action.
ASP.NET Web Forms
Each ASP.NET Web Forms Property Editor has two different controls that display a property in a Detail View and in Edit mode. These controls are listed below.
FileDataPropertyEditor
- View Mode Control
- A FileDataEdit control that uses the HtmlAnchor control to download a file.
- Edit Mode Control
- A FileDataEdit control that uses ASPxUploadControl to upload a new file and the HtmlAnchor control to download a file. In edit mode, FileDataEdit also contains the Change File and Clear buttons of the ASPxButton type.
- Description
- FileDataPropertyEditor displays the property’s value as a link with the file name. A user can click the link to download the file. In edit mode, the Change File and Clear buttons are shown. FileAttachmentController contains corresponding Actions.
FileDataPropertyEditor does not show the upload progress if the ImmediatePostDataAttribute is applied to the related FileData property or the IModelCommonMemberViewItem.ImmediatePostData option is enabled for the current View Item in Model Editor.