Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

An Open Source FileManagement Component Helps Developing File-required Systems Easier in .NET

4.00/5 (5 votes)
28 Mar 2010GPL34 min read 1  
The component is designed to save the effort on developing file-required systems through its easy APIs and integration with ASP.NET web development, e.g. a product requires attachments and thumbnails in a product management system.

Introduction

File Management mentioned in the article is a component implemented in RapidWebDev, which is designed to save the effort on developing file-required systems through its easy APIs and integration with ASP.NET web development, e.g. a product requires attachments and thumbnails in a product management system. You shall have basic impression from the following 4 minutes video on WHAT. Then the article introduces the data model, APIs and ASP.NET web control in detail.

Please click the screenshot of the Flash video below to watch it in the popup window.

Data Model

There are only 2 data tables for file management in RapidWebDev v1.52:

fm_Files: The table is used to store file head information only. The file stream is managed by the implementation of IFileStorageApi in file management component. There is a default implementation to store files in shared path.

Column Type Comment
FileId uniqueidentifier The auto generated id of a file
ApplicationId uniqueidentifier The SaaS application id which the file belongs to. RapidWebDev is a SaaS compatible solution.
Category nvarchar(31) The category of the file
Name nvarchar(255) The file name with extension name.e.g. thumbnail.jpg
ExtensionName nvarchar(15) The file extension name without dot. e.g. jpg
Description ntext The file description
BytesCount bigint The file size in bytes
CreatedOn datetime The date time when the file is created
UpdatedOn datetime The date time when the file is last updated
Version int The version number of the file which starts from 1. We keep potential to support file versioning in the future.

fm_FileBindings: The table is used to show the relationship between file and external objects.

Column Type Comment
ApplicationId uniqueidentifier The SaaS application id which the relationship belongs to. RapidWebDev is a SaaS compatible solution.
ExternalObjectId uniqueidentifier ID of any business objects which need file support. Like when you're developing a product management system with attachments and thumbnails support, the ProductId should be the external object id; when you're developing order system with attachments support, the OrderId should be the external object id.
FileId uniqueidentifier The id the file stored in data table fm_Files
RelationshipType nvarchar(64) the relationship type between external objects and files. Like when you're developing a product management system with attachments and thumbnails support, there should be 2 relationship types "Attachment" and "Thumbnail" between files and a product.

Internal APIs

There are only two interfaces for developers usually, IFileManagementApi and IFileBindingApi. The former one is used to manage file information and the latter one is used to manage relationship between files and external objects. In the following code snippet, the article ignores overload methods.

C#
/// <summary>
/// API to manage file information.
/// </summary>
public interface IFileManagementApi
{
    /// <summary>
    /// Save a file.
    /// </summary>
    /// <param name="fileUploadObject">
    /// The file uploading object which should include a readable file stream.
    /// </param>
    /// <returns>returns file head of the saved file.</returns>
    FileHeadObject Save(FileUploadObject fileUploadObject);

    /// <summary>
    /// Delete the file by id.
    /// </summary>
    /// <param name="fileId">The file id.</param>
    void Delete(Guid fileId);

    /// <summary>
    /// Load the file by id.
    /// </summary>
    /// <param name="fileId">The file id.</param>
    /// <returns></returns>
    FileHeadObject Load(Guid fileId);
}
C#
/// <summary>
/// API to manage relationship between files and external objects.
/// </summary>
public interface IFileBindingApi
{
    /// <summary>
    /// Bind the file with id of the external object.
    /// </summary>
    /// <param name="relationshipType"></param>
    /// <param name="externalObjectId"></param>
    /// <param name="fileId"></param>
    void Bind(string relationshipType, Guid externalObjectId, Guid fileId);

    /// <summary>
    /// Unbind the files associated with id of the external object 
    /// in special relationship type.
    /// </summary>
    /// <param name="externalObjectId"></param>
    /// <param name="relationshipType"></param>
    void Unbind(Guid externalObjectId, string relationshipType);

    /// <summary>
    /// Delete all files associated with id of the external object 
    /// in any relationship types 
    /// include both file entities and relationship.
    /// </summary>
    /// <param name="externalObjectId"></param>
    void DeleteBoundFiles(Guid externalObjectId);

    /// <summary>
    /// Get the files bound to id of the external object in special relationship type.
    /// </summary>
    /// <param name="externalObjectId"></param>
    /// <param name="relationshipType"></param>
    /// <returns></returns>
    IEnumerable<FileHeadObject> FindBoundFiles
		(Guid externalObjectId, string relationshipType);
}

ASP.NET Web Control

FileManagementControl is used to manage related files to a business object which locates at \\RapidWebDev.FileManagement\Web\FileManagementControl.cs. The control has the following properties:

Property PropertyType Comment
Uploadable bool True when the control is allowed to upload new files, defaults to True.
Deletable bool True when the control is allowed to delete existed files, defaults to True.
ReadOnly bool True when the control is readonly which cannot either upload new files or delete existed files, defaults to False.
MultipleUpload bool True when the control supports to upload multiple files, defaults to True.
MaximumFileCount int Maximum files can be managed by the control, defaults to 10. The property is useless when the property MultipleUpload equals to False.
FileCategory string Category of the managed files by the control, defaults to null.
EnableFileRemove<br />Confirmation bool Whether to show confirmation dialog for file removal, defaults to False.
FileUploadDialogTitle string Title of file uploading dialog, defaults to the globalized message "Resources.FileUploadDialogTitle".
RelationshipType string The relationship type between the external object and managing files, which will be saved into the table fm_FileBindings.
ExternalObjectId Guid Sets/gets the external object id which the managing files are associated with.

A sample code below demonstrates how to use the control:

ASP.NET
<My:FileManagementControl ID="ProductAttachments" 
   FileCategory="ProductAttachment" 
   RelationshipType="Attachment" runat="server" />
C#
void ButtonLoad_Click(object sender, EventArgs e)
{
    // load the assocated files with the external object.
    this.ProductAttachments.ExternalObjectId = ??;
}

void ButtonSave_Click(object sender, EventArgs e)
{
    // save the managing files with the external object.
    int associatedFileCount = this.ProductAttachments.AssociatedFileCount;
    this.ProductAttachments.ExternalObjectId = ??;
    this.ProductAttachments.Save();
}

Permission Integration

FileManagement integrates with permission through the interface IPermissionBridge in both HTTP file upload/download handler and FileManagementControl. There are three file operation permissions, Upload, Delete and Download. FileManagementControl renders Upload and Delete depending on whether the user has the related permission. HTTP file upload/download handler does the same. The permission value of file operation follows the format below:

  • Upload: FileManagement.{FileCategory}.Upload
  • Delete: FileManagement.{FileCategory}.Delete
  • Download: FileManagement.{FileCategory}.Download
C#
/// <summary>
/// The bridge which is used by RapidWebDev infrastructure for authorization.
/// </summary>
interface IPermissionBridge
{
    /// <summary>
    /// Returns true if the current user has any permissions in specified permission.
    /// </summary>
    /// <param name="permissionValue">Permission value.</param>
    /// <returns>
    /// Returns true if the current user has any permissions in specified permission.
    /// </returns>
    bool HasPermission(string permissionValue);
}   

What's RapidWebDev

Official Website for download: http://www.rapidwebdev.org

RapidWebDev is an infrastructure helps engineers to develop enterprise software solutions in Microsoft .NET easily and productively. It consists of an extendable and maintainable web system architecture with a suite of generic business model, APIs and services as fundamental functionalities needed in development for almost all business solutions. So when engineers develop solutions in RapidWebDev, they can have a lot of reusable and ready things then they can more focus on business logics implementation. In practice, we can save more than 50% time on developing a high quality and performance business solution than direct ASP.NET coding.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)