Back to Devexpress

IFileData Interface

expressappframework-devexpress-dot-persistent-dot-base-5d3fbccb.md

latest6.3 KB
Original Source

IFileData Interface

Declares members that implement the base functionality of file data objects.

Namespace : DevExpress.Persistent.Base

Assembly : DevExpress.Persistent.Base.v25.2.dll

NuGet Package : DevExpress.Persistent.Base

Declaration

csharp
public interface IFileData
vb
Public Interface IFileData

The following members return IFileData objects:

Remarks

If your custom type implements the IFileData interface, XAF can display objects of this type in the File Data or PDF Viewer property editor.

Tip

Refer to the following file for a sample FileData class implementation: %PROGRAMFILES%\DevExpress 25.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.EFCore\FileData.cs

Display FileData.cs code

csharp
using System;
using System.ComponentModel;
using System.IO;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;

namespace DevExpress.Persistent.BaseImpl.EF {
    [DefaultProperty(nameof(FileName))]
    public class FileData : BaseObject, IFileData, IEmptyCheckable {
        private Byte[] content;
        public virtual Int32 Size { get; set; }
        public virtual String FileName { get; set; }
        public virtual Byte[] Content {
            get { return content; }
            set {
                if(content != value) {
                    content = value;
                    Size = content != null ? content.Length : 0;
                }
            }
        }
        [Browsable(false)]
        public Boolean IsEmpty {
            get { return String.IsNullOrEmpty(FileName); }
        }
        public void LoadFromStream(String fileName, Stream stream) {
            FileName = fileName;
            Byte[] bytes = new Byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            Content = bytes;
            ObjectSpace.SetModified(this);
        }
        public void SaveToStream(Stream stream) {
            if(String.IsNullOrEmpty(FileName)) {
                throw new InvalidOperationException();
            }
            stream.Write(Content, 0, Size);
            stream.Flush();
        }
        public void Clear() {
            Content = null;
            FileName = "";
            ObjectSpace.SetModified(this);
        }
        public override String ToString() {
            return FileName;
        }
    }
}

To access a full file path, additionally implement the ISupportFullName interface in your class (works only in WinForms).

File Data Property Editor (Blazor, WinForms)Enable the File Attachments module to display objects that implement the IFileData interface in the File Data Property Editor.PDF Viewer Property Editor (Blazor, WinForms)Enable the Office Module and assign PdfViewerPropertyEditor to a file data property to display PDF files in the PDF Viewer.
Refer to the following topic for additional details: Use PDF Documents in Business Objects.

csharp
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;

namespace MainDemo.Module.BusinessObjects;

[DefaultClassOptions]
public class Resume : BaseObject {
    // ... 
    [FileTypeFilter("pdf-only", "PDF file", "*.pdf")]
    public virtual FileData File { get; set; }
    [EditorAlias(EditorAliases.PdfViewerPropertyEditor)]
    public FileData ResumeView => File;
}
csharp
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;

namespace MainDemo.Module.BusinessObjects;

[FileAttachment(nameof(File))]
[DefaultClassOptions]
public class Resume : BaseObject {
    private FileData file;
    // ...
    [Aggregated, ExpandObjectMembers(ExpandObjectMembers.Never)]
    [FileTypeFilter("pdf-only", "PDF file", "*.pdf")]
    public FileData File {
        get {
            return file;
        }
        set {
            SetPropertyValue(nameof(File), ref file, value);
        }
    }
    [EditorAlias(EditorAliases.PdfViewerPropertyEditor)]
    public FileData ResumeView => File;
}

See Also

IFileData Members

How to: Store file attachments in the file system instead of the database

GitHub repository: Store file attachments in Dropbox instead of the database

How to: Store file attachments in Dropbox instead of the database (video)

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

GitHub example: Use the File Attachment Module with a legacy database

DevExpress.Persistent.Base Namespace