I’m currently reading Stephen Walther’s new book: ASP.NET MVC Framework Unleashed
and have started to get in the mindset of how to do things in MVC. Tonight I was reading chapter 7, where he shows an example of uploading a file and saving it to the file system … which got me thinking that I needed to add some extension methods to DBFile to use the same code for saving to a database … so I did.
The controller code to upload a file and save to a database using DBFile (changeset 26172) would now look like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(HttpPostedFileBase upload)
{
// Save file
upload.SaveToDB();
return RedirectToAction("Create");
}
The extensions are in the DBFile.Core.Web namespace and add following to the HttpPostedFileBase class:
SaveToDB() (takes the file name off of the HttpPotedFileBase.FileName property)
SaveToDB(string fileName)
SaveToDB(bool overwrite)
SaveToDB(string fileName, bool overwrite)
I also added new underlying methods on the DBFile class that are used by the new extension methods, in case you don’t want to use the extension methods or need to check the int result coming back from the database:
int DBFile.Save(HttpPostedFileBase file)
int DBFile.Save(HttpPostedFileBase file, string fileName)
int DBFile.Save(HttpPostedFileBase file, bool overwrite)
int DBFile.Save(HttpPostedFileBase file, string fileName, bool overwrite)