ASP.NET File Manager - Create A Custom File System Provider (available now in v2011.1)

ASP.NET Team Blog
09 March 2011

Extend the DevExpress ASP.NET File Manager control with your own custom file system provider.

The DXperience v2011 volume 1 release is available now and you can create your own file system provider on top of the existing File System Provider.

ASP.NET File Manager

We added the ability to data bind the ASPxFileManager using datasources. And you can also extend and use a custom file system provider.

Read on to learn how to create your own file system provider to use with our ASP.NET File Manager control.

Default File System Providers

First, some background information: A File System Provider is an API to access to the virtual file system in the ASPxFileManager control.

FileSystemProviderBase_ClassThis API provides the definition of methods for getting a file and folder hierarchy. We also provide the methods for editing the FileManager's items like creating a folder, renaming files/folders, deleting files/folders, etc.

The File Manager control uses the provider for all operations in the file system. The ASPxFileManager has two predefined providers: PhysicalFileSystemProvider and DataSourceFileSystemProvider. These are providers created automatically inside the FileManager control based on chosen properties.

Custom File System Providers

To create your own custom provider, simply derive from a common class and override a few methods. Here are the 3 overall steps:

1. Create a descendant of the FileSystemProviderBase class or one of the predefined provider classes.

The class FileSystemProviderBase includes all the methods for working with the file system:

  • Most common methods: GetFiles and GetFolders, that returns list of files/folders for the specified parent
  • Editing methods: CreateFolder, RenameFolder, RenameFile, etc., used for performing operations with file system items
  • ReadFile - a method, used for getting a file Stream (used by the FileManager for creating a file's thumbnails and downloading)

2. Override those methods that are required for the task at hand. You don't need to override all the methods if you descend from another predefined provider.

For example, you can to override ReadFile method in DataSourceFileSystemProvider to get file content from storage outside the current datasource:

public class CustomHybridFileSystemProvider : DataSourceFileSystemProvider
{
    const string StoragePath = "D:\\FilesStorage";
 
    public CustomHybridFileSystemProvider(string rootFolder)
        :base(rootFolder) { }
 
    public override System.IO.Stream ReadFile(FileManagerFile file) {
        string filePath = GetPhysicalFilePath(file.RelativeName);
        return File.Exists(filePath) ? File.OpenRead(filePath) : Stream.Null;
    }
 
    string GetPhysicalFilePath(string relativeName) {
        return Path.Combine(StoragePath, relativeName);
    }
}

Therefore, you can do either of the following when creating your custom file provider:

  • Create a FileSystemProviderBase descendant and override all of its methods related to getting a file system hierarchy, and use only the relevant methods that are required for editing
  • Create a descendant of any of the predefined providers  and override only those methods that are required. For example, you can to override ReadFile method in DataSourceFileSystemProvider to get file content from storage outside the current datasource

3. Finally, tell the ASPxFileManager control which provider to use. Set the:

  • ASPxFileManager.SettingsDataSource.DataSourceID or ASPxFileManager.SettingsDataSource.DataSource properties, and the ASPxFileManager will create an instance of DataSourceFileSystemProvider. Otherwise, the ASPxFileManager will work with the standard physical file system provider
  • ASPxFileManager.CustomFileSystemProvider or ASPxFileManager.CustomFileSystemProviderTypeName properties, and the ASPxFileManager will use your custom provider instead of the default one
<dx:ASPxFileManager 
    ID="ASPxFileManager1" 
    runat="server" 
    CustomFileSystemProviderTypeName="WebApplication1.CustomHybridFileSystemProvider" 
    DataSourceID="DataSource1" >
    <Settings ThumbnailFolder="~\Thumb\" />
    <SettingsDataSource 
        KeyFieldName="Id" 
        ParentKeyFieldName="Pid" 
        NameFieldName="Name" 
        IsFolderFieldName="IsFolder" 
        LastWriteTimeFieldName="LastWriteTime" />
</dx:ASPxFileManager>

Available now in v2011 volume 1

The new file system providers in the DevExpress ASP.NET File Manager control are powerful and extensible.

And this new custom file system provider is available now in the DXperience v2011 volume 1 release.

What do you think of the new capability to create your own customer file system provider? Drop me a line below. Thanks!

Follow MehulHarry on Twitter

DXperience? What's That?

DXperience is the .NET developer's secret weapon. Get full access to a complete suite of professional components that let you instantly drop in new features, designer styles and fast performance for your applications. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Free DevExpress Products – Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We’ll be happy to follow-up.
Robbert Speet
Robbert Speet

Hé, that sounds pretty funcky !

I can think of a couple of things I can do with that.

Are you also exposing a couple of events concerning the file uploading like the FileUploadCompleted and FilesUploadCompleted and Validation event ?

9 March 2011
MagicRalf
MagicRalf

This is a great feature! I can' t wait till I can make my own custom file provider.

9 March 2011
Andreas Mummenhoff
Andreas Mummenhoff

Very good. I would like to have ASPxFileManager on WinForms. This would be very great.

10 March 2011
Roman Resh (DevExpress)
Roman Resh (DevExpress)

Hello, Don.

What would you like to do with these events? We have client-side FileUploading and FileUploaded events and server-side FileUploading event in FileManager. Do they suffice?

10 March 2011
Robbert Speet
Robbert Speet

Hi Roman,

I would like to make the uploaded filename seo friendly (so strip out all unwanted chars like spaces etc.)

Furthermore, I have a utility class which will scan the filebytes array to get the actual mimetype of the uploaded data to see if it is really a .pdf or a .doc or whatever, so people cannot tamper with the allowed uploaded file extensions.

With the standalone ASPxUploadControl, that works really great and is absolutely safe.

16 March 2011
Dmitry Voychenko
Dmitry Voychenko

Looks great!

Will it allow downloading finally?

16 March 2011
Dmitry Voychenko
Dmitry Voychenko
16 March 2011
Roman Resh (DevExpress)
Roman Resh (DevExpress)

Hello, Don.

We have a plan to add these properties to the server-side FileUploading event:

byte[] FileBytes

string FileName

Can these properties solve your tasks?

17 March 2011
Robbert Speet
Robbert Speet

Hi Roman,

That sounds ok!

There probably also should be:

bool IsValid

string ErrorText

And maybe something like:

bool CancelStore

The reason for this is that I might want to extract a zip file without storing the actual zip file in the upload folder

Before you released the ASPxFileManager, I had created my own file manager (much like yours) but for the uploading a used the separate ASPxUploadControl.

Next to the upload control, I also had 2 checkboxes:

[] Override existing file (if unchecked, then auto number name)

[] Uncompress archive (in case a zip file was uploaded)

It also had the mime-detection and seo-friendly renaming and all of it was done in the ASPxUploadControl.FileUploadComplete Event

17 March 2011
Roman Resh (DevExpress)
Roman Resh (DevExpress)

Hi, Don. Thanks for the detailed description of your control.

We have two properties (in v2010.2, too) to validate (check for viruses, mime-type checking, etc.) of upload files on the server side:

string ErrorText

bool Cancel

For now, ASPxFileManager doesn't have any support for custom buttons or checkboxes. We have a suggestion to provide it: S135919. Currently, you can add outer checkboxes and analyze their values on the server-side event FileUploading (for uncompress archive instead of upload archive file)

18 March 2011
Robbert Speet
Robbert Speet

Hi Roman,

Great stuff !

The checkboxes are not a problem. I'll figure a way out to place them somewhere, but the server-side events on the Filemanager is more important.

I'm looking forward for the 2011 release !

28 March 2011

Please login or register to post comments.