Project Description
DBFile is a utility library that uses the Provider pattern to help save files (or streams) to a database. Currently only a provider for SqlServer is provided.

The codebase for the starting point of this project is the .Net 3.5 port of a utility called Downstream that I put together several years ago (can read more about it at: http://www.jasonhaley.com/articles/downstream.htm.)

This is one of those utilities that does only one thing: make it easier to deal with files stored in a database. The goal is to create multiple providers that will remove the complexity of dealing with the database, for example the following code saves a file to the database when a user has uploaded a file using the FileUpload Asp.Net server control (or html input type=file):

using System;
using DBFile.Core.Web;

namespace DBFile.WebApp
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (IsPostBack)
			{
				Request.SaveFilesToDB();
			}
		}
	}
}

With an ASP.NET MVC application you can use the following code for uploading/downloading:
using DBFile.Core.Web.Mvc;
using DBFile.Core.Web;

namespace DBFile.MvcWebApp.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
                  
        public ActionResult Download()
        {
            // Pass in the filename to retrieve file from the database
            return this.FileFromDB("activity_10442705.tcx");

            // Or pass the filename and the content type
            //return this.FileFromDB("activity_10442705.tcx", "xml/text");

            // Or manually create the DBFileReult using the file
            //return new DBFileResult("activity_10442705.tcx", "xml/text");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Upload(HttpPostedFileBase upload)
        {
            // Saves an uploaded file to the database
            upload.SaveToDB();

            // Or manually set the name
            //upload.SaveToDB("somefile.txt");

            return RedirectToAction("Index");
        }

        public ActionResult Index()
        {
            return View();
        }
    }
}


Once the file is saved to the database, if you use the HttpModule to access the file by just adding it to the url. For example: if you upload a file with the path of "c:\testfile.jpg" and your web site url is: http://www.somesite.com, you will be able to access the file by using the url: "http://www.somesite.com/testfile.jpg".

DBFile also makes it easy to put files in the database for Console and WinForms apps. For example the following code will add the file foo.txt to the database:
DBFile.Core.DBFile.Move("C:\filed\foo.txt")

To get the file back out, you use some code like the following:
FileStream file = DBFile.Core.DBFile.Open("foo.txt");

If you need to check whether there is already a file with a given name saved in the database you use code like the following:
if (DBFile.Core.DBFile.Exists("foo.txt"))
// already exists
else
// not in the database yet

Extension method makes it easy to load an xml file into an XmlDocument, you just need code like the following:
NOTE: this is available in the Source Code download (on the source code tab) changeset 2098. It isn't currently in the Release zip off the home page.
string xmlText = "<root><ele1>text</ele1></root>";

// Save to DB
DBFile.Core.DBFile.Save("xmltest.xml", xmlText, "xml/text");

XmlDocument doc = new XmlDocument();
doc.LoadFromDB("xmltest.xml"); // Load the document from the database

The newest extension method makes it easy to load an xml file into an XDocument, you just need code like the following:
NOTE: this is available in the Source Code download (on the source code tab) changeset 17973. It isn't currently in the Release zip off the home page.
string xmlText = "<root><ele1>text</ele1></root>";

// Save to DB
DBFile.Core.DBFile.Save("xmltest.xml", xmlText, "xml/text");

XDocument doc = new XDocument().LoadFromDB("xmltest.xml"); // Load the document from the database

Features

Web Application Specific

  • Extension method to save files passed in the HttpRequest (by using the file upload control)
  • HttpModule to retrieve files from the database and rewrite the url to appear the file itself is being served
  • Open method for writing out to the HttpResponse, optionally as an attachement

ASP.NET MVC Application Specific

  • DBFileResult for downloading files
  • Extension methods to save files passed in on the HttpPostedFileBase class
  • Extension methods to retrieve files on the Controller class

Other

  • Ability to save to a database for multiple scenarios:
    • File on filesystem to database
    • Stream to database
    • Filename and text to a text file in database
  • Ability to Open a file from the database and return as:
    • Stream
    • File saved on file system (Move)
  • Ability to get a listing of the files saved in the database

Current providers

Database providers
Currently I've only implemented the DBFile.SqlServerProvider. This provider offers the option of using stored procedures or static sql. There are create table and create procedures scripts for both Sql2000, Sql2005 and Sql2008. Currently the provider is not very flexible, I'm looking at ways to make it more flexible (ie. ability to use an existing table or change the names of the table or stored procedures).

Other providers
FileSystemProvider - allows you to set a directory on a machine to utilize as a file repository.

AzureBlobProvider - allows you to use an AzureBlob storage location and account for storing and retrieving files.

WcfFileProvider - allows you to use the DBFile static methods to 'save' a file to another machine utilizing WCF over tcp. The settings in the config files are still in flux and not ready for production applications - but usint the WcfFileService.Host as the service you should be able to get the demo up and going pretty quick.

FtpProvider - allows you to setup an ftp site to use a a repository of files.

Last edited Dec 19, 2009 at 3:48 AM by JasonHaley, version 14