Problem:

Managing files web applications should be quick and easy and most importantly, consistent. The traditional way to store files is on the file system or within a RDBMS (SQL SERVER, Oracle, MySql) or on SharePoint. However what if you were developing several applications that shared a million files and each was 5MB. Which solution would you choose? This is commonly the case for large content driven sites (Music Stores, video sharing sites, content management systems, etc).

Solution:

In providing a solution I would implement a SOA based File Service, and this article will guide you to making your own File Service. The reasons that I believe a File Service works are:
  • Single point of storage for data
  • Can be used by multiple applications
  • Single source for security
  • Easy to de-duplicate files
  • Clear separation between binary data and metadata
  • Efficient file system usage ensuring that director retrieval is fast
  • Efficient IO usage using multiple disks (per share)

Data Structure:

Firstly we need to create a table to store our file information. Most columns on the table shown in Figure 1 are self explanatory and for possible enhancements see the "Future Enhancements" section below.

Building a File Service
Figure 1

Solution Structure:

  1. Create a new Solution in Visual Studio 2008, targeting the .NET Framework 3.5
  2. Create a new Class Library called "FileServiceCore"
    1. Add a new class called "FileHandlerFactory.cs"
    2. Add a new class called "FileHandlerLinq.cs"
    3. Add a new class called "FileDistributor.cs"
    4. Add a new LINQ to SQL Classes called "FileService.dbml"
    5. Using the Server Explorer create a new connection to the database that contains the "Files" table described above. You should be able to drag and drop the table onto the designer.
  3. Create a new Website called "FileService"
    1. Add a project reference to the FileServiceCore
    2. In the website root add a new generic handler called "ImageHandler.ashx"
    3. In the website root add a new generic handler called "DownloadHandler.ashx"
  4. Create a new website called "SampleWebsite"
    1. Add a project reference to the FileServiceCore

Your solution should now look like figure 2 below.

Building a File Service
Figure 2

Logic:

Now that you have created your Data Structure and Visual Studio solution we will need to add the application logic to each of the classes (e.g. FileHandlerFactory.cs).

FileDistributor.cs

The FileDistributor class is responsible for distributing files to the file system. It contains a single static method that calculates a network file path for a FileId.
Building a File Servicenamespace FileServiceCore

FileHandlerLinq.cs

The FileHandlerLinq class is a static class that each calling application can use to Insert/Select files from the Files table. For the purpose of this example update and deletes have been omitted.
FileHandlerFactory.cs
The FileHandlerFactory is a simple factory class that determines if the browser should handle the file or if the file should be downloaded by the client as an attachment. In this example the factory is simple and should be customized for your own needs.
Building a File Serviceusing System;
Building a File Service
using System.Web;
Building a File Service
namespace FileServiceCore

DownloadHandler.ashx and ImageHandler.ashx
The Generic ImageHander and DownloadHandlers are almost identical and their sole purpose is to parse the Uri for the FileId token. Once the FileId token is collected the generic handler calls for a file handler from the File Handler Factory.
Building a File Service<%@ WebHandler Language="C#" Class="DownloadHandler" %>
Building a File Service
using System;
Building a File Service
using System.Web;
Building a File Service
public class DownloadHandler : IHttpHandler

Default.aspx
The below sample .aspx page shows a common usage to add data to the file service.


Code Behind:
--------------------------------------------------------
Building a File Serviceusing System;
Building a File Serviceusing System.Globalization;
Building a File Servicepublic partial class _Default : System.Web.UI.Page
Building a File Service{
Building a File Serviceprotected void Page_Load(object sender, EventArgs e)
Building a File Service{
Building a File Service}
Building a File Serviceprotected void UploadFileButton_Click(object sender, EventArgs e)
Building a File Service{
Building a File Serviceif (UploadFileControl.HasFile)
Building a File Service{
Building a File Service// get the acutal file name
Building a File Servicestring fileName = UploadFileControl.PostedFile.FileName;
Building a File ServicefileName = fileName.Substring(fileName.LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase) + 1);
Building a File Service// insert our file and redirect back to this page with the fileId appended to the querystring
Building a File ServiceFileServiceCore.File fileEntity = FileServiceCore.FileHandlerLinq.Insert(
Building a File ServicefileName,
Building a File ServiceUploadFileControl.PostedFile.ContentType,
Building a File ServiceUploadFileControl.PostedFile.InputStream);
Building a File Service// redirect back to our page
Building a File ServiceResponse.Redirect(string.Format(CultureInfo.InvariantCulture, "default.aspx?FileId={0}", fileEntity.FileId));
Building a File Service}
Building a File Service}
Building a File Service}
Building a File Service

Future Enhancements:

The following list highlights some of the possible enhancements that can be made to the service, depending on your needs:
  1. Public/Private flags for files
  2. Cache headers for the ImageHandler
  3. Resizing/cropping/effects for the ImageHandler
  4. Store statistics for file usage (last accessed, times accessed, etc)
  5. Demotion of non used files to another storage medium (tape, DVD, etc)
  6. File locking, to ensure files aren't accidentally deleted.

Assumptions:

  1. There is an available NAS/SAN with enough storage capacity for your needs
  2. The IIS account running the File Service has appropriate permissions to read/write to the NAS/SAN

本文转载 kane-nina
http://www.kanebarton.com/Samples/FileService/Default.aspx

相关文章:

  • 2018-04-15
  • 2021-05-25
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
猜你喜欢
  • 2021-06-14
  • 2022-12-23
  • 2021-05-20
  • 2021-06-30
  • 2022-01-01
  • 2021-08-03
  • 2021-11-21
相关资源
相似解决方案