一个开源文件管理组件,帮助您在.NET中更轻松地开发需要文件功能的系统
该组件通过其简单的API和与ASP.NET Web开发的集成,旨在节省开发需要文件功能的系统的工作量,例如,一个产品管理系统需要附件和缩略图。
引言
文章中提到的文件管理是RapidWebDev中实现的一个组件,它通过其简单的API和与ASP.NET Web开发的集成,旨在节省开发需要文件功能的系统的工作量,例如,一个产品管理系统需要附件和缩略图。您可以通过以下4分钟的视频对“是什么”有一个基本的印象。然后,文章将详细介绍数据模型、API和ASP.NET Web控件。
请点击下方Flash视频的截图,在弹出窗口中观看。
Data Model
RapidWebDev v1.52 中只有两个文件管理数据表
fm_Files:该表仅用于存储文件头信息。文件流由文件管理组件中的IFileStorageApi 实现来管理。提供了一个默认实现,用于将文件存储在共享路径中。
| Column | 类型 | 注释 | 
| FileId | uniqueidentifier | 文件的自动生成ID | 
| ApplicationId | uniqueidentifier | 文件所属的SaaS应用程序ID。RapidWebDev是一个兼容SaaS的解决方案。 | 
| 类别 | nvarchar(31) | 文件的类别 | 
| 名称 | nvarchar(255) | 带扩展名的文件名。例如thumbnail.jpg | 
| ExtensionName | nvarchar(15) | 不带点的文件扩展名。例如jpg | 
| 描述 | ntext | 文件描述 | 
| BytesCount | bigint | 文件大小(字节) | 
| CreatedOn | datetime | 文件创建的日期时间 | 
| UpdatedOn | datetime | 文件最后更新的日期时间 | 
| 版本 | int | 文件的版本号,从1开始。我们保留了未来支持文件版本控制的可能性。 | 
fm_FileBindings:该表用于显示文件与外部对象之间的关系。
| Column | 类型 | 注释 | 
| ApplicationId | uniqueidentifier | 关系所属的SaaS应用程序ID。RapidWebDev是一个兼容SaaS的解决方案。 | 
| ExternalObjectId | uniqueidentifier | 需要文件支持的任何业务对象的ID。例如,当您开发一个支持附件和缩略图的产品管理系统时, ProductId 应该是外部对象ID;当您开发一个支持附件的订单系统时,OrderId 应该是外部对象ID。 | 
| FileId | uniqueidentifier | 文件中存储的文件ID,位于数据 表fm_Files中 | 
| RelationshipType | nvarchar(64) | 外部对象和文件之间的关系类型。例如,当您开发一个支持附件和缩略图的产品管理系统时,文件和产品之间应该有两种关系类型:“ Attachment”和“Thumbnail”。 | 
内部API
通常只有两个接口供开发人员使用:IFileManagementApi 和IFileBindingApi。前者用于管理文件信息,后者用于管理文件与外部对象之间的关系。在下面的代码片段中,文章省略了重载方法。
/// <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);
}
/// <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控件
FileManagementControl 用于管理与业务对象相关的配置文件,位于\\RapidWebDev.FileManagement\Web\FileManagementControl.cs。该控件具有以下属性
| 属性 | PropertyType | 注释 | 
| Uploadable | bool | 当控件允许上传新文件时为 True,默认为True。 | 
| Deletable | bool | 当控件允许删除现有文件时为 True,默认为True。 | 
| ReadOnly | bool | 当控件为只读模式时为 True,此时无法上传新文件也无法删除现有文件,默认为False。 | 
| MultipleUpload | bool | 当控件支持上传多个文件时为 True,默认为True。 | 
| MaximumFileCount | int | 控件可以管理的最大文件数,默认为 10。当属性MultipleUpload 等于False时,此属性无效。 | 
| FileCategory | 字符串 | 控件管理的文件的类别,默认为 null。 | 
| EnableFileRemove | bool | 是否为文件移除显示确认对话框,默认为 False。 | 
| FileUploadDialogTitle | 字符串 | 文件上传对话框的标题,默认为全局化消息“ Resources.FileUploadDialogTitle”。 | 
| RelationshipType | 字符串 | 外部对象与管理文件之间的关系类型,将保存到 fm_FileBindings表中。 | 
| ExternalObjectId | Guid | 设置/获取管理文件关联的外部对象ID。 | 
以下示例代码演示了如何使用该控件
<My:FileManagementControl ID="ProductAttachments" 
   FileCategory="ProductAttachment" 
   RelationshipType="Attachment" runat="server" />
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();
}
权限集成
FileManagement 通过HTTP文件上传/下载处理程序和FileManagementControl中的IPermissionBridge接口集成权限。有三种文件操作权限:上传、删除和下载。FileManagementControl根据用户是否拥有相关权限来渲染上传和删除。HTTP文件上传/下载处理程序也执行相同操作。文件操作的权限值遵循以下格式
- Upload:- FileManagement.{FileCategory}.Upload
- Delete:- FileManagement.{FileCategory}.Delete
- Download:- FileManagement.{FileCategory}.Download
/// <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);
}   
什么是RapidWebDev
官方下载网站:http://www.rapidwebdev.org
RapidWebDev是一个基础设施,帮助工程师在Microsoft .NET中轻松高效地开发企业级软件解决方案。它包含一个可扩展且可维护的Web系统架构,以及一套通用的业务模型、API和服务,这些是几乎所有业务解决方案开发所需的基本功能。因此,当工程师使用RapidWebDev开发解决方案时,他们可以获得许多可重用且现成的东西,从而能够更专注于业务逻辑的实现。在实际应用中,与直接编写ASP.NET代码相比,我们可以节省超过50%的时间来开发高质量、高性能的业务解决方案。




